Form Styles

Form Styles

Chapter 2: Forms introduced the important dialog-related properties: ControlBox, FormBorderStyle, HelpButton, MaximizeBox, MinimizeBox, ShowIcon, and ShowInTaskbar. By default, a new form shows the control box, is sizable, doesn't show the help button, can be minimized and maximized, and is shown in the shell's task bar. For a main window, these are fine settings, and they yield a form that looks like the one in Figure.

1. Typical Main Window Form Settings

A typical modal dialog, on the other hand, is more likely to hide both the minimize and the maximize boxes, show the help button, hide the icon, and not show up in the task bar (the parent is already there), as shown in Figure.

2. Typical Sizable Modal Form Settings

Interestingly, even though SizeGripStyle is set to its default of Auto, the size grip is automatically shown for modal forms (Figure), but it is not shown for main windows (Figure). Also, while the ControlBox property remains true when the border style is changed to FixedDialog, the icon is not shown when ShowIcon is set to true, as you can see in Figure.

3. Typical Fixed Modal Form Settings

Clearly, Windows Forms has its own ideas about what to show along the edge of your form, sometimes disregarding your preferences. Except for FormBorderStyle, typical modeless form settings are just like the sizable modal form settings (from Figure) except that calling Show instead of ShowDialog causes the size grip to go away.

These examples should serve most of your needs, although it's possible to vary form properties to get a few more variations. For example, you can use the border styles FixedToolWindow and SizableToolWindow to show the caption in miniature (handy for floating tool strip or tool box windows).

Deciding on Modal Versus Modeless at Run-Time

If you'd like your form to change its settings based on whether it's being shown modally, check its Modal property:

void ModalOrModelessDialog_Load(object sender, EventArgs e) {
  if( this.Modal ) {
    // Show as a fixed-sized modal dialog
    this.FormBorderStyle = FormBorderStyle.FixedDialog;
  else {
    // Show as a sizable modeless dialog
    this.FormBorderStyle = FormBorderStyle.Sizable;

Depending on whether the form is shown using ShowDialog or Show, the Modal property is true or false, respectively. However, because the way a form is shown isn't known until after it has been created, you can't use the Modal property when it is inspected from a form's constructor, because it is always false at that time. However, you can use the Modal property value during and after the Load event.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows