Solved

Add control and event

Posted on 2000-03-21
21
457 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 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

12 Experts available now in Live!

Get 1:1 Help Now