TheDeveloperBlog.com


GroupBox in Windows Forms

GroupBox. In a GroupBox other controls may be placed. When designing user interfaces in Windows Forms, you can use this control to create a square shape where you can place other controls. This creates an important level of visual continuity.


Start. To begin, you need to add the GroupBox control to your Windows Forms application. You can do this by opening the Toolbox and double-clicking the GroupBox icon, or dragging the icon to your window.

Next: You should resize, or preferably dock, the GroupBox. Then you can add more controls inside the GroupBox by dragging.


Dock. For most user interfaces in Windows Forms, using the docking or anchoring options for controls is ideal. These options allow you to tie the size of the control to the size of an enclosing control or the window itself.

Tip: This means you can have fluidly-resizing windows that always look great, regardless of a system's setup.

Giving users the ability to resize windows is important if some of them use different font sizes or have poor vision. To dock the GroupBox, please select Properties on the GroupBox in the Visual Studio designer.

Then: Scroll to Dock, and click one of the six buttons. These are easy to figure out, but I describe them here.

GroupBox options

Top, Bottom, Left, Right:

These will push the control to the selected edge.

Center:

This will expand the control to fill the entire window.
Shown in the GroupBox in this tutorial.

None:

The default, this will disable docking.


Options. Every control in Windows Forms has many options for changing visual effects and also the text shown. For the GroupBox, you will want to change the Text property. In the screenshot, the Text property was changed to "Dot Net Perls."

Note: Dock is also an important property. Please see the above notes on docking.


Events. It is also possible to add event handlers to execute code when events occur in the GroupBox. Honestly, I haven't found much use of events on the GroupBox. But perhaps in some cases they are useful.

Tip: Typically, when designing user interfaces, it is a good idea to create abstractions that can be reused.

And: These designs should not rely on a specific user interface toolkit for logic.


Summary. The GroupBox control is not an exciting control in the Windows Forms platform, but is certainly a useful one. It can help you organize a clean and usable interface for your program, and is helpful on preferences windows and dialogs.