VB created dynamically control author: Unknown create data-driven form the so-called data-driven form is generated depending on the requested data to the appropriate form. For example, suppose you now have a database, some of which must be displayed according to the login identity, the authorization level can browse and modify the content of these fields; the authorization level The general user can only browse data in these fields Users with low authorization levels cannot browse content in these fields. To do this, you have to use the VB to dynamically add controls. Dynamic Creating Controls Whether you want to create a type of data-driven form, you must know how to create a control at runtime. You can do this through the array of controls, but the ADD method provided by the VB6's Controls collection is more powerful and more flexible. With this method, you don't need to place the instance of the control in the form on the form. In fact, you can even create a program that does not exist at all time when compiling. This method is also very simple: 'Declaring a form-level variable DIM WIM WITHEVENTS TXTTOTBOX () Create a new TextBox control set txttotal = controls.add ("vb.textbox", "txttotal")' When the control moves to the place you need TXTTotal.Move 1000, 800, 1200, 300 'creation, all controls are invisible txttotal.visible = trueEnd Sub Please note the second parameter of the add method: assign to the control name. From the code readability, this name is usually the same as the variable name. You can use this name to get the appropriate control from the Controls collection or remove the control. For example: controls.remove "txttotal" plus the withevents key when the variable declaration, even if the form does not exist in the design, you can write code for the control. The method described above is only suitable for solving VB built-in controls. For example, when you want to add a TreeView control, VB will ask you to prove that you have got legitimate authorization to create an instance of the control. In other words, VB wants to prove that this control is bought, not borrowed from other programs attached to the control. To prove that you have a few ways to authorize: Place a control on the form. This is also the easiest way. You don't have to display this form at all. Add related controls into the toolbox, then delete "Remove Information about Unused ActiveX Controls" in the "Engineering Properties" dialog box. Add an element to the licenses collection. For example: debug.print licenses.add ("msmask.maskedededbox") Just create a control and not enough to create a data-driven form, only knowing that the dynamic creation control is not enough. For example: Now you have to create a form that automatically generates a control according to different fields in the database. The form may create a single line text box, which is different from the length of the field; you may also create a radio button or a check button to display a Boolean field; it is even possible to create a multi-line text box to display the note field. The first question you need to solve is that the multiline property of the text box control is read-only, only when designing. Fortunately, Microsoft's Microsoft Windowless Controls 6.0 can solve this problem. This set of controls includes a lightweight TextBox, ComboBox, ListBox, Checkbox, Optionbutton, CommandButton, and two ScrollBar controls. The greatest difference between these controls and VB built-in correspondence is that all properties of these controls are readable when running.
This control group can be found in the CommON / Tools / VB / Winless folder in the VB installation CD. Use the following code to create a multi-line text box: Dim WitHextPrivate Sub CreateEditor () set txtEditor = controls.add ("mswless.wltext", "txtEditor") TXTeditor.Move 0, 0, 4000, 4000 TXTeditor.MultiLine = true txtEditor.scrollbars = wlboth txtEditor.visible = trueEnd Sub another problem is more complicated: How to assign a unique band to each reference to the newly created control in advance WitHevents keyword variable. In other words, it is to program the event of the newly created control. The premise is that you don't know how many controls created when you design. Using an object array is obviously not possible, because you cannot declare an object array with WitHevents keywords; a worse case is that a variable is defined as AS Control or AS Object, because it is not possible to use WitHevents. The problem is derived from us to capture an object array event at runtime. So we can only take a curve to save the country. The code to write may be more interesting than you, but this solution is very interesting, it is worth doing this. We need two auxiliary modules to capture events, named ControlItems and ControlItem. ControlItems is a collection class that saves the ControlItem object and its quantity. This amount is equal to the number of controls that you want to program. Each instance of the ControlItem class captures the event generated by the control, and then calls the procedure in the ControlItems collection class it belongs, and finally triggered the event in the form and performs the code in the event by ControlItems in the form. The whole process is shown below: The event captures multiple controls is simple, assuming that you want to capture the Validate event from all the controls that are dynamically added to the form. To accomplish this, the ControlItems collection class must show the event to the parent form and ready to receive notifications from their sub-ControlItem classes to trigger events. Code is as follows: Event Validate (CtrlItem As ControlItem, Cancel As Boolean) Private m_ControlItems As New Collection 'to add a new item ControlItem Function Dim newItem As New ControlItem newItem.Init ctrl Add (ctrl As Control) As ControlItem, Me set to' Add to private class m_ControlItems.Add newItem 'returns the new item to the caller Set Add = newItemEnd FunctionFriend Sub Notify_Validate (item As ControlItem, Cancel As Boolean) RaiseEvent Validate (item, Cancel) End SubControlItem class must be captured from the dynamic added to the form The event of the control, and notifies the ControlItems collection class to which it belongs. Obviously, the ControlItem class must have a variable defined with the WitHevents keyword to reference the real control. This means you can't declare the variable as AS Control or AS Object.
If you decide that the control that is dynamically added in the form does not use the VB built-in control, the solution is quite simple. You only need to declare the variable to the VBControlExtender type. For creating data-driven forms, controls that do not use VB built-in are not a big deal. Declaring the variable as VBControleXtender, and add the WitHevents keyword, you can capture Validate, GotFocus, LostFocus, DragDrop, and Dragover. If you want to capture more more events, you can use ObjectEvent. The following is ControlItem class module code: Public WithEvents Ctrl As VBControlExtender 'ControlItems objects Dim m_Parent As ControlItemsSub Init belongs (ctl As Object, parnt As ControlItems) Set Ctrl = ctl Set m_Parent = parntEnd SubPrivate Sub Ctrl_Validate (Cancel As Boolean)' ControlItems m_Parent.Notify_Validate Me notification class belongs, CancelEnd Sub code into the following form, can capture events added dynamically generated by the control: Dim WithEvents CtrlItems as New ControlItemsPrivate Sub cmdCreateControls_Click () Dim ctrl as control ' Create two text boxes and add them to the ControlItems collection? Set ctrl = controls.add ("mswless.wltext", "one") ctrl.move 100, 200, 1000, 300 ctrl.visible = true ctrlitems.add ctrl ' Note that you can use the same variable Set ctrl = Controls.Add ( "MSWLess.WLText", "Two") ctrl.Move 100, 800, 1000, 300 ctrl.Visible = True CtrlItems.Add ctrlEnd SubPrivate Sub CtrlItems_Validate (CtrlItem As ControlItem Cancel as boolean 'refusing to empty string - Note how to reference controls? If ctrlitem.ctrl.text = "" "" "" "" "" "" "" "" "" "" "" The Cancel = TrueEnd Sub now solves the most difficult part, to create a data-driven form becomes Simple ******************************************************** ***** *********************************************************** ************************************************************************************ ) Parameter description Object is required. An object expression, its value is an object in the list. PROGID must be. A string that identifies the control. Most controls of ProgID can be determined by viewing an object browser. The ProgID of the control is composed of the library and class of the control.
For example, the ProgID of the CommandButton control is VB.CommandButton. In the case where the ProgID is displayed in different, Visual Basic will display an error message including the correct progID. Name is necessary. A string for identifying a member of the collection. Container is optional. An object reference, it specifies the container of the control. If you do not specify or be NULL, the default value is the container belonging to the Controls collection. By specifying this parameter, a control can be placed in any existing container control (such as the Frame control). User control or ActiveX document can also be used as a container. Example: // Add a CommandButtonPrivate Sub form_load () form1.controls.add "vb.commandbutton", "cmdok", "cmdok", "cmdok", "cmdok .visible = true .width = 500 .caption =" confirmation (& y) "End Withend Sub Key: When you add an uncomposed control to a license, you must also add this control license keyword before using the Add method when you add an existing (deployed) application. Add an unfounded control at runtime: You can also use the Add method to dynamically add a control that is not referenced in the project. ("Uncounted" control is not currently in Toolbox). To do this, you must also add the control's license keyword to the license collection. In the following example, add controls itself before adding the controls of a license key: Option ExplicitPrivate WithEvents extCtl As VBControlExtenderPrivate Sub Form_Load () Licenses.Add "prjWeeks.WeeksCtl", "xydsfasfjewfe" Set extCtl = Form1.Controls.Add ( " Prjweeks.WeeksctL "," CTL1 ") Extctl.visible = true 'The control is invisible by default.end Sub However, in order to program an event of such an unbound control, you must use the withevents keyword to declare an object variable to vbcontrolextender object ( As above), and set the object variable to the reference returned by the Add method. Then use the ObjectEvent event of the VBControleXtender object to program the event of the control. Here is a simple example. Option ExplicitDim WithEvents objExt As VBControlExtender 'variable declaration Extender Private Sub LoadControl () Licenses.Add "Project1.Control1", "xydsfasfjewfe" Set objExt = Controls.Add ( "Project1.Control1", "myCtl") objExt.Visible = TrueEnd SubPrivate Sub extobj_objectEvent (Info As EventInfo) 'Events using the SELECT CASE programming control.