Solved

Add control and event

Posted on 2000-03-21
21
459 Views
Last Modified: 2013-12-25
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?
0
Comment
Question by:JOK
  • 15
  • 6
21 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640274
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640280
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640287
Oh... a bit daft. An enormous button...
and a spurious declaration...

Ah well, you get the idea (I hope)
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640412
That was completely wrong. It works for listviews but not for buttons!
0
 
LVL 3

Author Comment

by:JOK
ID: 2640413
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
 
LVL 10

Accepted Solution

by:
caraf_g earned 150 total points
ID: 2640417
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640423
Re. Lounge

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

<g>
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640431
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
 
LVL 3

Author Comment

by:JOK
ID: 2640455
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640510
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:JOK
ID: 2640527
Its a dynamic number. :(
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640536
No worries, I'll work out a quick example for you.
0
 
LVL 3

Author Comment

by:JOK
ID: 2640553
Adjusted points from 100 to 150
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640556
1 - Add a .bas module

Option Explicit
Global gobjButtonCollection As clsButtonCollection

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640559
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640563
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640566
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2640570
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
 
LVL 3

Author Comment

by:JOK
ID: 2640755
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
 
LVL 3

Author Comment

by:JOK
ID: 2640764
I had to mess with it some because of the way I was adding buttons, but it worked with very few modifications.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2641014
Great. Glad I could help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now