Recipe 4.1. Creating and Adding Controls at Runtime


Problem

You need to add one or more controls to a form dynamically at runtime. You used to do something similar to this in Visual Basic 6.0 using control arrays, but those do not exist in Visual Basic 2005.

Solution

Sample code folder: Chapter 04\DynamicControls

You can add any control to a form at runtime just by creating an instance of it. Your code can define the initial properties, such as the location of the control on the form, at runtime. You can also connect events for these runtime controls to event handlers, although the handler methods must exist at design time. (Technically, it's possible to write a method at runtime, but such programming is beyond the scope of this book and is generally frowned upon.)

Discussion

To test this method of dynamically creating controls, start by creating a new Windows Forms application and add the following source code to Form1's code template:

 Private Sub ShowTheTime(ByVal sender As System.Object, _       ByVal e As System.EventArgs)    ' ----- Display the time in the text box, if it exists.    Dim theTextBox As TextBox    ' ----- Locate and update the text control.    theTextBox = Me.Controls("TimeTextBox")    If (theTextBox IsNot Nothing) Then       theTextBox.Text = Now.ToLongTimeString( )    End If End Sub Private Sub Form1_Load(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Load    ' ----- Add controls at runtime.    Dim dynamicText As TextBox = Nothing    Dim dynamicButton As Button    ' ----- Dynamically add a text box control to the form.    dynamicText = New Windows.Forms.TextBox    dynamicText.Name = "TimeTextBox"    dynamicText.Location = New System.Drawing.Point(8, 8)    dynamicText.Size = New System.Drawing.Size(232, 20)    dynamicText.TabIndex = 0    Me.Controls.Add(dynamicText)    ' ----- Dynamically add a button control to the form.    dynamicButton = New Windows.Forms.Button    dynamicButton.Location = New System.Drawing.Point(144, 32)    dynamicButton.Size = New System.Drawing.Size(99, 23)    dynamicButton.Text = "Get Time"    dynamicButton.UseVisualStyleBackColor = True    dynamicButton.TabIndex = 1    Me.Controls.Add(dynamicButton)    ' ----- Connect the button to an event handler.    AddHandler dynamicButton.Click, AddressOf ShowTheTime End Sub 

When you run the program, you will see two controlsa TextBox control and a Button controlmagically appear on the previously empty form. Clicking the button calls the prewritten event handler, which inserts the current time into the text box, as shown in Figure 4-1.

In Visual Basic 6.0, if you wanted to add a control to a form at runtime it was necessary to create a design-time control just like it, and create a dynamic copy of it at run-time. This was due, in part, to the special design-time method used to record form controls. If you opened up the .frm file for a Visual Basic 6.0 form, you would see nonVisual Basic code at the top of the file that defined the controls and the form itself.

Figure 4-1. Dynamically generated controls on a form


In Visual Basic 2005, all form controls, and even the form itself, exist through standard object creation. When Form1 appears on the screen in a running program, it's because somewhere in your program there is code that creates a new instance of Form1 and calls its Show method:

 (New Form1).Show 

Although you add controls to your form using the Visual Studio Form Designer, Visual Studio actually generates runtime code for you that dynamically creates the controls and adds them to the form. All this code is generally hidden in the form's designer file. To view this file, select the Project Show All Files menu command, and expand the branch for one of your forms in the Solution Explorer panel. By default, Form1's designer file is named Form1.Designer.vb.

To create the source code for this project, we added a TextBox and a Button control to the form and then opened the designer code file. We then copied selected lines from that file and made slight adjustments before pasting that code into the form's Load event handler. Finally, we deleted the design-time controls from the form.

See Also

Recipes 4.2 and 4.3 also discuss features that are replacements for Visual Basic 6.0 control arrays.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

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