Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle events from dynamically generated controls?

Status
Not open for further replies.

aryajur

Programmer
Jul 2, 2003
45
0
0
US
Hi,
I am trying to generate controls dynamically at runtime. I am able to do that using the code below but I don't have a clue how to handle events from these controls.
Any help would really be appreciated.

Sub test4()
' Note: a textbox has no caption, hence the caption string is left null
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.checkbox.1", 2, 2, 175, 20, "1")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.textbox.1", 2, 2, 175, 20, "")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.label.1", 2, 2, 175, 20, "3")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.optionbutton.1", 2, 2, 175, 20, "4")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.togglebutton.1", 2, 70, 175, 20, "5")
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.commandbutton.1", 2, 70, 175, 20, "6")
frm1.Show
End Sub


Sub AddCheckBox(intCount As Integer, strControl As String, intLeft As Integer, intTop As Integer, _
intWidth As Integer, intHeight As Integer, strCaption As String)
'Dim ctl As Control
Dim mycmd As Control
'strControl = "Forms.checkbox.1"
Set mycmd = Controls.Add(strControl)
mycmd.Left = intLeft
mycmd.Top = intTop + 20 * intCount
mycmd.Width = intWidth
mycmd.Height = intHeight
If strCaption <> "" Then
mycmd.Caption = strCaption
End If
mycmd.Visible = True
' Me.ScrollBars = fmScrollBarsVertical
With Me
.ScrollBars = fmScrollBarsVertical
.ScrollHeight = intTop + 40 + 20 * intCount
End With
End Sub
 
You need:
- a WithEvents declaration in class/object module,
- an event handler for this variable,
- an assignment of the created control to variable declared with WithEvents.
Only native events can be used in this way (i.e. for commandbutton control only events that are shown for commandbutton class in object browser. No inherited events such as Control events on the userform or OLEObject (OLEControl) events on spreadheet/document.

combo
 
Actually, you do not always have to use WithEvents. It depends on exactly what you are doing, and trying to do.

Say you have commandbutton that is going to create a checkbox.
Code:
Private Sub CommandButton1_Click()
Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.checkbox.1", 2, 2, 175, 20, "1", _
   InputBox("Type name to be used."))
End Sub
This is from your code, with the added
Code:
InputBox("Type name to be used.")
This passes the result of the inputbox as a string into your code, using it as the .Name of the control:
Code:
Sub AddCheckBox(intCount As Integer, strControl As String, intLeft As Integer, intTop As Integer, _
intWidth As Integer, intHeight As Integer, strCaption As String, [b]CurrentName As String[/b])
    Dim mycmd As Control
    Set mycmd = Controls.Add(strControl)
   With mycmd
      .Left = intLeft
      .Top = intTop + 20 * intCount
      .Width = intWidth
      .Height = intHeight
    [b]  .Name = CurrentName[/b]
   End With
    If strCaption <> "" Then
        mycmd.Caption = strCaption
    End If
    mycmd.Visible = True
    With Me
        .ScrollBars = fmScrollBarsVertical
        .ScrollHeight = intTop + 40 + 20 * intCount
    End With
End Sub

Say you entered "Yadda" into the InputBox. There is now a checkbox with the name of "Yadda". Therefore:
Code:
Private Sub CommandButton3_Click()
MsgBox Me.Controls("Yadda").Value
End Sub
will in fact return the result of the checkbox (checked or unchecked).

Now, it is true that you need to know the name you gave the control. That is why I say it depends on exactly what you are doing, and trying to do.

If you know in advance what they could be - say you are making a ONE textbox, ONE checkbox available - then you could have an array of names and make the user choose one.

You could make a Collection and add any new control names to it, and then use the collection.

Your code seem to be hard-coded. It creates specific controls.

"Forms.checkbox.1", 2, 2, 175, 20, "1"
"Forms.textbox.1", 2, 2, 175, 20, ""
"Forms.label.1", 2, 2, 175, 20, "3"
"Forms.optionbutton.1", 2, 2, 175, 20, "4"
"Forms.togglebutton.1", 2, 70, 175, 20, "5"
"Forms.commandbutton.1", 2, 70, 175, 20, "6"

So it should not be too hard to gather, and be able to use, names...if you give them names...which you should.

If you have the names, then you can do stuff with them. especially if you know what you ARE going to name them.

Say you create the textbox, and you KNOW you want to name it txtClientName. Then:
Code:
    Call frm1.AddCheckBox(frm1.Controls.Count, "Forms.textbox.1", 2, 2, 175, 20, "", _
 "txtClientName")
then the creating Sub will create a textbox named "txtClientName".
Code:
    Dim mycmd As Control
    Set mycmd = Controls.Add(strControl)
   With mycmd
      .Left = intLeft
      .Top = intTop + 20 * intCount
      .Width = intWidth
      .Height = intHeight
    [b]  .Name = CurrentName[/b]
   End With
There is no problem in having event code already written...if you so choose. Again...it depends on what you are actually doing, and trying to do.

And speaking of that, why are you creating controls dynamically? I am not arguing against it, but I have found that it is rarely actually needed.

More often the requirements is actually for the visibility of controls.

If condition X, then controls A, B, F, G, H, P are visible.

If condition Y, then controls A, B, F, Q, Z are visible.

This can be adjusted a number of ways.

The userform itself can be easily resized dynamically to show (or hide) areas with controls.

The .Visible of controls themselves can be used.

A Multi-Page can hold a large number of controls with a very efficient use of space.

The point being if the controls are already there (but visible or not), then you have the events already there to use.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top