Add control and event

I'm adding controls to a form at run time using
Form.Controls.Add("VB.CommandButton","myname")

myname is uique.

What I want is to add an event to do something when the new button is clicked. How can I do this?

Should I add the button to a Control Array?
LVL 3
JOKAsked:
Who is Participating?
 
caraf_gConnect With a Mentor Commented:
This does work for a button.

Option Explicit

Private WithEvents objButton As VB.CommandButton
Private Sub Form_Load()
     
    Set objButton = Controls.Add("VB.CommandButton", "MyControl")
    With objButton
        .Left = 150
        .Top = 150
        .Width = 1110
        .Height = 375
        .Visible = True
        .Caption = "Click Me!!"
    End With
     
End Sub

Private Sub objButton_Click()

MsgBox "hi"

End Sub
0
 
caraf_gCommented:
Here's an example of a dynamically loaded list view. It's not a button but I'm sure you'll get the idea.

Option Explicit

Private WithEvents ctlExtender As VBControlExtender
Private objY As Object
Private Sub Form_Load()
    Dim objX As Control
    Licenses.Add "MSComctlLib.ListViewCtrl.2"
    Set objX = Controls.Add("MSComctlLib.ListViewCtrl.2", "MyControl")
    Set ctlExtender = objX
    With ctlExtender
       .Visible = True
       .Top = 1000
       .Left = 1000
       .Height = 3000
       .Width = 3000
    End With
    Set objY = objX.object
    objY.ColumnHeaders.Add , , "Test"
    objY.View = 3
    objY.ListItems.Add , , "Text"
    objY.ListItems.Add , , "Text"
    objY.ListItems.Add , , "Text"
    objY.ListItems.Add , , "Text"
    objY.ListItems.Add , , "Text"
    objY.ListItems.Add , , "Text"
   
End Sub

Private Sub ctlExtender_ObjectEvent(Info As EventInfo)
   ' Program the events of the control using Select Case.
Select Case Info.Name
Case "DblClick"
    ' Check User name value.
    Dim objX As Object
    Set objX = objY.SelectedItem
    If objX Is Nothing Then
        MsgBox "no item selected"
    Else
        If Not objX.Selected Then
            MsgBox "no item selected"
        Else
            MsgBox objX.Text
        End If
    End If
    ' Other cases now shown
Case Else ' Unknown Event
    ' Handle unknown events here.
End Select
End Sub


0
 
caraf_gCommented:
Option Explicit

Private WithEvents ctlExtender As VBControlExtender
Private objY As Object
Private Sub Form_Load()
    Dim objX As Control
     
    Set objX = Controls.Add("VB.CommandButton", "MyControl")
    Set ctlExtender = objX
    With ctlExtender
       .Visible = True
       .Top = 1000
       .Left = 1000
       .Height = 3000
       .Width = 3000
    End With
     
End Sub

Private Sub ctlExtender_ObjectEvent(Info As EventInfo)
   ' Program the events of the control using Select Case.
Select Case Info.Name
Case "Click"
    ' Button was clicked!
    ' Other cases now shown
Case Else ' Unknown Event
    ' Handle unknown events here.
End Select
End Sub
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
caraf_gCommented:
Oh... a bit daft. An enormous button...
and a spurious declaration...

Ah well, you get the idea (I hope)
0
 
caraf_gCommented:
That was completely wrong. It works for listviews but not for buttons!
0
 
JOKAuthor Commented:
Who let you out of the lounge? The code reformatted my hard drive and brought down our LAN.

Actually, the first code worked, but the second gave me a runtime error (#13) data type mismatch both when I tried your 2nd example and when I modified it for my app.

I'm not sure what the problem is.
0
 
caraf_gCommented:
Re. Lounge

I do roam the corridors of EE and pop up going "boo" behind people's backs occasionally.

<g>
0
 
caraf_gCommented:
Basically, the reason for the difference is that a VB button is a "simple" vb control. You can therefore access it directly.

The ListView is accessed through an OCX control, and is therefore used through the Extender object.

That's why I'm also using an objY variable in that case to access methods and properties that are specific to the ListView object.
0
 
JOKAuthor Commented:
Its sorta working. I'm adding more than 1 control and the event only fires for the last button created. I'll increase points if you can help me make it work for every button (VB isn't my forte).
0
 
caraf_gCommented:
Depends. It can be resolved in two different ways, depending on whether you've got a fixed number of buttons, or a dynamic number.

If you have a fixed number of buttons, you can simply do this:

Private WithEvents objButton1 As VB.CommandButton
Private WithEvents objButton2 As VB.CommandButton
Private WithEvents objButton3 As VB.CommandButton
Private Sub Form_Load()
       
    Set objButton1 = Controls.Add("VB.CommandButton", "Button1")
    With objButton1
        .Left = 150
        .Top = 150
        .Width = 1110
        .Height = 375
        .Visible = True
        .Caption = "Button1"
    End With
    Set objButton2 = Controls.Add("VB.CommandButton", "Button2")
    With objButton2
        .Left = 150
        .Top = 555
        .Width = 1110
        .Height = 375
        .Visible = True
        .Caption = "Button2"
    End With
    Set objButton3 = Controls.Add("VB.CommandButton", "Button3")
    With objButton2
        .Left = 150
        .Top = 960
        .Width = 1110
        .Height = 375
        .Visible = True
        .Caption = "Button3"
    End With
       
End Sub

Private Sub objButton1_Click()

MsgBox "hi1"

End Su

Private Sub objButton2_Click()

MsgBox "hi2"

End Su

Private Sub objButton3_Click()

MsgBox "hi3"

End Su
0
 
JOKAuthor Commented:
Its a dynamic number. :(
0
 
caraf_gCommented:
No worries, I'll work out a quick example for you.
0
 
JOKAuthor Commented:
Adjusted points from 100 to 150
0
 
caraf_gCommented:
1 - Add a .bas module

Option Explicit
Global gobjButtonCollection As clsButtonCollection

0
 
caraf_gCommented:
2 - Add a class module. Called clsButton

Option Explicit

Public WithEvents objButton As VB.CommandButton

Private Sub objButton_Click()

gobjButtonCollection.ButtonClick CInt(objButton.Tag)

End Sub
0
 
caraf_gCommented:
3 - Add another class module. Called clsButtonCollection

Option Explicit

Public Event Click(Index As Integer)

Public Sub ButtonClick(Index As Integer)

RaiseEvent Click(Index)

End Sub
0
 
caraf_gCommented:
4 - Add a form with one normal button on it. Command1

Option Explicit
Private WithEvents mobjButtonCollection As clsButtonCollection
Private colButtons As Collection

Private Sub Command1_Click()

Dim objButton As clsButton
Set objButton = New clsButton
colButtons.Add objButton
Set objButton.objButton = Controls.Add("VB.CommandButton", "Button" & CStr(colButtons.Count))
With objButton.objButton
    .Left = 150
    .Top = Command1.Top + (colButtons.Count) * Command1.Height
    .Height = Command1.Height
    .Width = Command1.Width
    .Caption = "Button" & CStr(colButtons.Count)
    .Tag = CStr(colButtons.Count)
    .Visible = True
End With

End Sub

Private Sub Form_Load()

Set gobjButtonCollection = New clsButtonCollection
Set mobjButtonCollection = gobjButtonCollection
Set colButtons = New Collection

End Sub

Private Sub mobjButtonCollection_Click(Index As Integer)

MsgBox "button " & CStr(Index) & " was clicked"

End Sub
0
 
caraf_gCommented:
5 That's it... Every time you click Command1, another button is added to the form. When you click one of these buttons it'll tell you which one was clicked.

This example was very quickly knocked up and probably not very elegant. But hopefully it'll get you going in the right direction.

Hope this helps.


Pino
0
 
JOKAuthor Commented:
Would have increase points further and given a A+ (instead just a measley A) if you had've used the same variable names I used. ;)

This works, 'tho.

TKS!
0
 
JOKAuthor Commented:
I had to mess with it some because of the way I was adding buttons, but it worked with very few modifications.
0
 
caraf_gCommented:
Great. Glad I could help.
0
All Courses

From novice to tech pro — start learning today.