Adding Controls to a Form at Run Time


Adding Controls to a Form at Run Time

Throughout this book, you've added objects to forms by using the Toolbox and the Designer. However, as the previous exercise demonstrated, you can also create Visual Basic objects on forms at run time, either to save development time (if you're copying routines you have used before) or to respond to a current need in the program. For example, you might want to generate a simple dialog box containing objects that process input only under certain conditions.

Creating objects is very simple because the fundamental classes that define controls in the Toolbox are available to all programs. Objects are declared and instantiated (or brought into being) by using the Dim and New keywords. The following program statement shows how this process works when a new button object named button1 is created on a form:

Dim button1 As New Button

After you create an object at run time, you can also use code to customize it with property settings. In particular, it's useful to specify a name and location for the object because you didn't specify them manually by using the Designer. For example, the following program statements configure the Text and Location properties for the new button1 object:

button1.Text = "Click Me" button1.Location = New Point(20, 25)

Finally, your code must add the following new object to the Controls collection of the form where it will be created. This will make the object visible and active in the program:

form2.Controls.Add(button1)

If you are adding the new button to the current form (that is, if you are adding a button to Form1 and your code is located inside a Form1 event procedure), you can use the Me object instead. For example,

Me.Controls.Add(button1)

adds the button1 object to the Controls collection of the current form. When you do this, be sure that a button1 object doesn't already exist on the form you are adding it to. (Each object must have its own, unique name.)

You can use this process to add any control in the Toolbox to a Visual Basic form. The class name you use to declare and instantiate the control is a variation of the name that appears in the Name property for each control.

The following exercise demonstrates how you can add a Label control and a Button control to a new form at run time. The new form will act as a dialog box that displays the current date.

Create new Label and Button controls

  1. Click the Close Project command on the File menu, and then create a new application project named My Add Controls.

  2. Display the form (Form1.vb).

  3. Use the Button control to add a button object to the form, and then change the Text property of the button object to Display Date.

  4. Double-click the Display Date button to display the Button1_Click event procedure in the Code Editor.

  5. Type the following program code:

    'Declare new form and control objects Dim form2 As New Form Dim lblDate As New Label Dim btnCancel As New Button 'Set label properties lblDate.Text = "Current date is: " & DateString lblDate.Size = New Size(150, 50) lblDate.Location = New Point(80, 50) 'Set button properties btnCancel.Text = "Cancel" btnCancel.Location = New Point(110, 100) 'Set form properties form2.Text = "Current Date" form2.CancelButton = btnCancel form2.StartPosition = FormStartPosition.CenterScreen 'Add new objects to Controls collection form2.Controls.Add(lblDate) form2.Controls.Add(btnCancel) 'Display form as a dialog box form2.ShowDialog()

    This event procedure displays a new form containing a label object and a button object on the screen. The label object contains the current date as recorded by your computer's system clock (returned through DateString). The Text property of the button object is set to “Cancel”.

    As I mentioned earlier, you add controls to a form by declaring a variable to hold the control, setting object properties, and adding the objects to the Controls collection. In this exercise, I also demonstrate the Size and CancelButton properties for the first time. The Size property requires a Size structure. The New keyword is used to immediately create the Size structure. The CancelButton property allows the user to close the dialog box by pressing Esc or clicking the Cancel button. (The two actions are equivalent.)

  6. Click the Save All button, and specify the c:\vb05sbs\chap14 folder as the location.

    TIP
    The complete Add Controls program is located in the c:\vb05sbs\chap14\add controls folder.

  7. Click the Start Debugging button to run the program.

    Visual Basic displays the first form on the desktop.

  8. Click the Display Date button.

    Visual Basic displays the second form on the desktop. This form contains the label and button objects that you defined by using program code. The label object contains the current date, as shown here:

    graphic

  9. Click the Cancel button to close the new form.

  10. Click the Display Date button again.

    The new form appears as it did the first time.

  11. Press Esc to close the form.

    Because you set the CancelButton property to the btnCancel object, clicking Cancel and pressing Esc produce the same result.

  12. Click the Close button on the form to end the program.

    The program stops, and the development environment returns.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

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