JOK
asked on
Add control and event
I'm adding controls to a form at run time using
Form.Controls.Add("VB.Comm andButton" ,"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?
Form.Controls.Add("VB.Comm
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?
Option Explicit
Private WithEvents ctlExtender As VBControlExtender
Private objY As Object
Private Sub Form_Load()
Dim objX As Control
Set objX = Controls.Add("VB.CommandBu tton", "MyControl")
Set ctlExtender = objX
With ctlExtender
.Visible = True
.Top = 1000
.Left = 1000
.Height = 3000
.Width = 3000
End With
End Sub
Private Sub ctlExtender_ObjectEvent(In fo 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
Private WithEvents ctlExtender As VBControlExtender
Private objY As Object
Private Sub Form_Load()
Dim objX As Control
Set objX = Controls.Add("VB.CommandBu
Set ctlExtender = objX
With ctlExtender
.Visible = True
.Top = 1000
.Left = 1000
.Height = 3000
.Width = 3000
End With
End Sub
Private Sub ctlExtender_ObjectEvent(In
' 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
Oh... a bit daft. An enormous button...
and a spurious declaration...
Ah well, you get the idea (I hope)
and a spurious declaration...
Ah well, you get the idea (I hope)
That was completely wrong. It works for listviews but not for buttons!
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Re. Lounge
I do roam the corridors of EE and pop up going "boo" behind people's backs occasionally.
<g>
I do roam the corridors of EE and pop up going "boo" behind people's backs occasionally.
<g>
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.
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.
ASKER
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).
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.CommandBu tton", "Button1")
With objButton1
.Left = 150
.Top = 150
.Width = 1110
.Height = 375
.Visible = True
.Caption = "Button1"
End With
Set objButton2 = Controls.Add("VB.CommandBu tton", "Button2")
With objButton2
.Left = 150
.Top = 555
.Width = 1110
.Height = 375
.Visible = True
.Caption = "Button2"
End With
Set objButton3 = Controls.Add("VB.CommandBu tton", "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
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.CommandBu
With objButton1
.Left = 150
.Top = 150
.Width = 1110
.Height = 375
.Visible = True
.Caption = "Button1"
End With
Set objButton2 = Controls.Add("VB.CommandBu
With objButton2
.Left = 150
.Top = 555
.Width = 1110
.Height = 375
.Visible = True
.Caption = "Button2"
End With
Set objButton3 = Controls.Add("VB.CommandBu
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
ASKER
Its a dynamic number. :(
No worries, I'll work out a quick example for you.
ASKER
Adjusted points from 100 to 150
1 - Add a .bas module
Option Explicit
Global gobjButtonCollection As clsButtonCollection
Option Explicit
Global gobjButtonCollection As clsButtonCollection
2 - Add a class module. Called clsButton
Option Explicit
Public WithEvents objButton As VB.CommandButton
Private Sub objButton_Click()
gobjButtonCollection.Butto nClick CInt(objButton.Tag)
End Sub
Option Explicit
Public WithEvents objButton As VB.CommandButton
Private Sub objButton_Click()
gobjButtonCollection.Butto
End Sub
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
Option Explicit
Public Event Click(Index As Integer)
Public Sub ButtonClick(Index As Integer)
RaiseEvent Click(Index)
End Sub
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.CommandBu tton", "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
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.CommandBu
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
MsgBox "button " & CStr(Index) & " was clicked"
End Sub
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
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
ASKER
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!
This works, 'tho.
TKS!
ASKER
I had to mess with it some because of the way I was adding buttons, but it worked with very few modifications.
Great. Glad I could help.
Option Explicit
Private WithEvents ctlExtender As VBControlExtender
Private objY As Object
Private Sub Form_Load()
Dim objX As Control
Licenses.Add "MSComctlLib.ListViewCtrl.
Set objX = Controls.Add("MSComctlLib.
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(In
' 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