Adding Controls to Forms

Within the Visual Studio .NET Forms Designer, controls may be placed on the surface of any container object, such as Windows forms or controls such as Panel, GroupBox, and TabControl. You may also add controls to a form programmatically.

graphics/tip_icon.gif

When creating controls programmatically, you must remember to associate them to a parent container control. If you don't do this, controls will be created but will not be displayed.


Adding Controls with the Windows Forms Designer

Within the Forms Designer interface, you may drag and drop controls from the Toolbox window, which is activated by Ctrl+Alt+X or through the View menu. Figure 2.10 shows the Toolbox window.

Figure 2.10. The Toolbox window.

graphics/02fig10.jpg

From the Toolbox, you have three different ways to add controls to a container object:

  • Method 1 In the first method, you can select a control from the Toolbox window and draw it on the container surface by following these steps:

    1. Select a control by clicking the control's icon in the Toolbox window (refer to Figure 2.10).

    2. Release the mouse button and move the mouse pointer to the position on the container where you want to draw the control.

    3. Hold down the mouse button and draw a rectangle on the container surface to indicate the size and position for the control instance.

  • Method 2 With the second method, you drag the control directly from the Toolbox window to the desired location on the form. Follow these steps:

    1. Select a form or other container control where you wish to add a control.

    2. Drag the control's icon from the Toolbox window and drop it at the desired location on the container control. The control will be added with its default size.

  • Method 3 With the third method, you can add the control to the form by double-clicking in the Toolbox window. Follow these steps:

    1. Select a form or other container control where you wish to add a control.

    2. Double-click the control's icon in the Toolbox window. This will add the control to the top-left corner of the form or other container control in its default size. You can later use the mouse or properties of the control to modify its position and size.

Adding Controls Programmatically

You may add controls to a container object programmatically. When doing so, you should remember to add code to take care of three things:

  • Create a private variable to represent each of the controls you want to place on the form.

  • In the form's constructor, place code to instantiate each control and to customize each by using its properties, methods , or events.

  • Add each control to the form's control collection.

When creating controls programmatically, you should also specify the location and size of the controls numerically . You may add a control to a Windows form programmatically by following these steps:

  1. Add a new Windows form to your Visual Basic .NET project.

  2. Switch to code view and add the following variables , just below the Windows Form Designer's generated code:

     ' Create variables to hold controls Dim lblName, lblPassword As Label Dim txtName, txtPassword As TextBox Dim btnLogin As Button 
  3. Expand the Windows Form Designergenerated region. Modify the New() procedure as follows :

     Public Sub New()     MyBase.New()     'This call is required by the Windows Form Designer.     InitializeComponent()     'Add any initialization after the InitializeComponent() call     ' specify the form's size     Me.ClientSize = New System.Drawing.Size(272, 182)     'Set up the label the Name textbox     Dim lblName As Label = New Label()     lblName.Text = "Name: "     ' Specify the location for the control     ' the default location will be (0, 0) otherwise     lblName.Location = New Point(16, 16)     ' Set up the label for the Password textbox     Dim lblPassword As Label = New Label()     lblPassword.Text = "Password: "     lblPassword.Location = New Point(16, 80)     ' Set up a Name textbox     Dim txtName As TextBox = New TextBox()     txtName.Location = New Point(152, 16)     ' Set up a Password textbox     Dim txtPassword As TextBox = New TextBox()     txtPassword.Location = New Point(152, 80)     txtPassword.PasswordChar = "*"     ' Set up a command button     Dim btnLogin As Button = New Button()     btnLogin.Text = "Login"     btnLogin.Location = New Point(96, 128)     ' Add controls to the form     ' Method 1: Specify the current form as parent container for a control     lblName.Parent = Me     ' Method 2: Add a control to form's control collection     Me.Controls.Add(txtName)     ' Method 3: Add an array of controls to form's control collection     Me.Controls.AddRange(New Control() {lblPassword, txtPassword, btnLogin}) 
  4. Set the form as the startup object for the project. To do this, right-click the project node in Solution Explorer and select Properties. You can select the startup object on the General properties page.

  5. Run the project. The form will be displayed as shown in Figure 2.11.

    Figure 2.11. A form with controls loaded programmatically.

    graphics/02fig11.jpg

The examples in this section add controls to a Windows form, but the same concepts will work with any of the container controls. A container control has a property named Controls that is a collection of Control objects that the container control contains. When you add or remove a control to or from a container, it is added to or removed from the container's Controls collection.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net