1. Computing & Technology

Discuss in my forum

Formatting Controls - Using the Primary Control

A really quick tip that you might not know about.

By , About.com Guide

Visual Studio has excellent ability to help you format controls on forms. (Unfortunately, VB.NET Express doesn't provide the Format menu. ASP.NET and WPF have more limited Format menus. This article applies to Windows Forms projects.) But there are just a few corners that can be confusing, especially when you're trying to format a complicated form. This Quick Tip explains them. These formatting ideas aren't technically complicated. They're just something that might help you get your work done faster and with better looking results.

A form doesn't have to have a lot of controls to be complicated. It's what you do with them. You are probably already familiar with the ability of Visual Studio to align and adjust the position of controls on a form. If not, here's a sample screenshot that will give you the idea:

--------
Click Here to display the illustration
--------

Windows, and Visual Studio, do a lot more to make forms look good on any device today. For example, you can develop on one machine ... like your desktop machine with the huge monitor ... and deploy on another ... like one of those glorified telephones. Windows will "autoscale" the forms to relative to either the DPI resolution or the scale of the fonts. But you have to get it right on your desktop to begin with.

A key concept is the primary control. If you drag a selection box across several controls (left click a blank place on the form and drag), the control that is selected first is the primary control. In the illustration above, this is the GroupBox in the lower right corner because I started from that corner when I used a selection box to select the controls. The visual indicator is that the sizing handles are little white boxes instead of little black boxes. The same thing happens when you select a group one at a time by holding the Ctrl or Shift key down as you click inside the control. Click again to un-select.

To arrange your controls most efficiently, decide which one will be the primary control and remember to select that one first. Menu commands like Make Same Size and Horizontal or Vertical Spacing will use the size or spacing of the primary control to set all of the others.

It can also speed up your work a little to remember that if a whole group of controls have the same property, you can set that property for all of them by selecting the whole group and then setting the property once in design view.

The illustration above shows three labels beside the three buttons. (The label for Button1 is on the right side.) You will sometimes use labels with no Text in them because you don't want them to be visible until some other event takes place. You can lose track of where these are because they don't normally show up. One technique to see where they are is to drag over a whole area as I did to select them.

While we're talking about positioning controls, remember that you can position controls exactly in the same space accomplish some special effect. The four textboxes on the right side are all positioned exactly on top of each other. You can control which one is displayed using the BringToFront() method. This code gets the RadioButton selected and uses that to bring the appropriate TextBox to the top of the pile. (RB1 through RB4 are the RadioButton controls.)


Dim CurrentRB As RadioButton
Dim RB As New Microsoft.VisualBasic.Collection()
Private Sub Form1_Load(
    ByVal sender As System.Object, ByVal e As System.EventArgs
    ) Handles MyBase.Load
    RB.Add(RB1)
    RB.Add(RB2)
    RB.Add(RB3)
    RB.Add(RB4)
End Sub
Private Sub RB_CheckedChanged(
    ByVal sender As System.Object,
    ByVal e As System.EventArgs
    ) Handles RB1.CheckedChanged, RB2.CheckedChanged,
              RB3.CheckedChanged, RB4.CheckedChanged
    CurrentRB = CType(sender, RadioButton)
    CycleRB()
End Sub
Private Sub CycleRB()
    Select Case True
        Case CurrentRB.Equals(RB1)
            TB1.BringToFront()
        Case CurrentRB.Equals(RB2)
            TB2.BringToFront()
        Case CurrentRB.Equals(RB3)
            TB3.BringToFront()
        Case CurrentRB.Equals(RB4)
            TB4.BringToFront()
    End Select
End Sub

"Hey!" (I hear you shouting) "You could do the same thing by just assigning different Text with the same control! Why the convoluted code to do something so simple?"

Yes, but you could use any control in this code ... like individual MonthCalendar controls. All of the properties, not just Text, will be preserved. If you're intererested, you can download the code ... which also includes a Timer that automatically cycles through the differeent TextBoxes ... by clicking here.

©2012 About.com. All rights reserved.

A part of The New York Times Company.