SSW Foursquare

Rules to Better Windows Forms - 3 Rules

Did you know that Windows Forms was recently updated with the release of .NET 8? See the changes on Microsoft Learn!


This category has been archived
Archived Reason: Category contains outdated references to windows forms
  1. Do you know Windows Forms should have a minimum size to avoid unexpected UI behavior

    If windows form does not setup a minimum size, your users could have unpredictable form behaviour as seen below:

    Figure: Bad example - Unexpected window form

    Therefore, a standard has been built to ensure Windows forms have a minimum size.

    Figure: Good Example - User friendly window form

  2. Do you know how to format your MessageBox code?

    You should always write each parameter of MessageBox in a separate line. So it will be more clear to read in the code. Format your message text in code as you want to see on the screen.

    Private Sub ShowMyMessage()
     MessageBox.Show("Are
     you sure you want to delete the team project """ + strProjectName
     + """?" + Environment.NewLine + Environment.NewLine + "Warning:
     Deleting a team project cannot be undone.", strProductName + "
     " + strVersion(), MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)

    Figure: Bad example of MessageBox code format

    Private Sub ShowMyMessage()
     MessageBox.Show( _
     "Are you sure you want to delete the team project """ + strProjectName + """?"
     _ + Environment.NewLine _ +
     Environment.NewLine _ +
     "Warning: Deleting a team project cannot be undone.", _
     strProductName + " " + strVersion(), _
     MessageBoxButtons.YesNo, _
     MessageBoxIcon.Warning, _
     MessageBoxDefaultButton.Button2)
    End Sub

    Figure: Good example of MessageBox code format

  3. Do your Windows Forms have border protection?

    Border protection helps us design Windows Forms properly without placing controls too near to the border. Maintain a consistent alignment makes the Windows Forms look better, especially on designing wizard forms where all forms have the same size.

    Figure: Good example - Good border protection on a form at run time. The only problem is you would have to imagine these blue lines to get consistency

    Border protection in action:

    Figure: Bad example - Controls placed very near to the border and not aligned correctly

    Figure: Good example - All controls are in the border protection area and aligned correctly

    Figure: Design mode

    The way to implement border protection (the 2 vertical red lines) is implement it in the base form or base user control, and all other forms and user controls inherit the base class to get consistent border protection lines.

    private void BaseForm_Paint(object sender, PaintEventArgs e)
                {
                    // Draw border protection lines
                    if (this.DesignMode)
                    {
                       Pen pen = new Pen(Color.Red);
                       e.Graphics.DrawLine(pen,
                                23, 0, 23, this.Height);
                                e.Graphics.DrawLine(pen, this.Width - 23, 0, this.Width - 23, this.Height);
                    }
                }

    Q&A

    Why don't we put a panel on the form and set the form DockPadding property which does a similar thing?

    • Adding more panels docking to a form reduces the performance significantly because of the extra SuspendLayout and ResumeLayout calls.
    • In certain cases we might really want a control to stick at the border, if we use DockPadding Property, we can't make any exceptions. And still, these red lines actually just act like a ruler to help us easily see whether the controls are aligned nicely.
We open source.Loving SSW Rules? Star us on GitHub. Star
Stand by... we're migrating this site to TinaCMS