Building Dynamic Forms at Runtime VB Help

There are situations when you won’t know in advance how many instances of given control may be requirei:l on a Form. This isn’t very common, but if you’ writing a data entry application and you want .to work with many tables of a database, you’ll have to be especially creative. Since every table consists of  fields, it will be difficult to build a single Form to accommodate all the pose ble tables a user may throw at your application.

In these situations, it is possible to design dynamic Forms, which are population at runtime. The simplest approach is to create more controls than you’ll ever  and set their VISibleproperty to False at design time. At runtime, you can display  the controls by switching their Visible property to True. As you know already, quick-and-dirty methods are not the most efficient ones. The proper method tc create dynamic Forms at runtime is described in this section .

Just as you can load (existing) Fbrms with the Load statement, you can also load new controls on a Form. The controls to be loaded don’t need to exist on the Porm: they can be members of an array of controls. At design time, you can create one control and make it the first member of an array. To do so, assign the value 0 to its Index property at design time. Figure 4.11 shows the CLoad project, which
demonstrates this technique

The Form of Figure 4.11 is a data entry Form and the fields are placed on the Form at runtime. The CLoad project, in this chapter’s folder on the CD, builds the same Form every time it’s executed, but in a practical situation, you would adjust the fields to reflect the structure of a table or a structure defined by the user on another Form. .
To design the data entry Form of the CLoad application, you must place a Label and a TextBox control on the Form. Place them near the top of the. Form, so that they can be used as guides for aligning the remaining controls. These two controls will remain invisible at all times. Then complete the following steps: .
1. Name the Label control Labels and set its Index property to O.
2. Set its Alignment proJ>erty to 1(Right Justify).
3. Name the TextBox control TextBoxes and set its Index to O.
4. Then select both controls with the mouse and set their Visible property to False.
So far, you’ve created the structure necessary for loading new controls at rumtime. These controls will be placed on the Form in pairs with the Voad command and they’ll be aligned with the two controls placed on the Form at design time. The code behind the Fill This Form button is shown in Code 4.10

Creating New Controls at Runtime

The arrays Captions and Sizes hold the captions of the various fields and their lengths in number of characters. These two arrays don’t change in the CLoad application. However, you can either extract these values from a table in a database or let the user design the structure of the desired record and then use it to construct the arrays. It’s a simple method for creating data entry Forms that can bereused in many situations. By the way, this is how many WIZards work. The code then places another pair of controlson the Form, aligns them with the previous ones, adjusts the size of the TextBox control according to the value of the corresponding element of the Sizes array, and sets the caption of the Label control to the value of the corresponding element of the Caption array. Finally, it displays them by setting their VlSible property to True. Controls placed. on a Form with the Load statement are invisible by default. Even if the first items in a control array were visible, the controls loaded with the Load statement would be invisible, so you must always turn on the Visible property from within your code. This subroutine relies on the placement of ~heinitial controls on the Form. There are more ways to position the controls on the Form, but this is probably the simplest method. Place a few controls on the Form at design time, and then use them as guides for aligning the new controls at runtime. You should open the Cl.oad’ project on the CD and see how it works. You can modify the project to accommodate ather types of fields, such as”] rue/False fields (which should be mapped to CheckBox controls), dates, and so on. To complete the CLoad application, you should also add a few Command buttons, including the code to handle the data entered by the user (valid itc themsave to ra ndom-access, file, and so on).


A unique characteristic of the Windows user interf ice is the ability of the user to grab a control and drop it on another. This featu ;(, is called drac-and-drop and is used extensively in the Desktop. Nearly every .trm on the Desktop can be dragged and dropped on various other items, su. b is the Recycle Bin, the printers; folders, and so on, The same techniques can be used in applicatir n :-p()” ex. mpl- , the Form in Figure 4.12 contains the three file controls tha: let tl.e user sele.t and vi, ,v any folder on the disk. The two lists (Move to Temp and Move to Tap. ) aft.’destinations for various files. You could provide buttons t” rnovr files from the control to either list, but the most con venien I way t, I mow files to the have firstBox controls on the Form is to drag-and-drop then.

Open the ListDrop project to test-drive the application. To specify the files to be copied to the Temp folder or the tape drive (these features aren’t implemented in the code, but you can easily add the code to actually move or copy the selected files), drag them from the FileListBox control (middle window) to the correspending List. To undo an action, you can simply drag a file back. When a filename is moved from the file list to one of the two ListBox controls, the file isn’t removed from the file control (you have to delete the file to remove it from the . FileListBox control). However, when you move a selected filename from the Move to Temp or Move to Tape lists back to the FileListBox control, the name of the file is removed from the corresponding list, but it’s not added again to the FileListBox control.
Load and run the Listlzrop application and check out its ease of operation and functionality. Compare it with any other user interface d,esign based on more traditional controls, such as Command buttons, and you will see that drag-and-drop features can significantly enhance your application’s interface. User interfaces based on drag-and-drop operations aren’t common, but when an application can benefit from this type of interface, you should implement it. As you will see, doing so is quite easy and based on a small number of properties, methods, and events

Posted on November 4, 2015 in Working with Forms

Share the Story

Back to Top
Share This