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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic optionbutton_click

Status
Not open for further replies.

erikgismo

Programmer
Joined
Nov 22, 2006
Messages
3
Location
GB
I have a loop that each time generate a optionbutton dynamic:

Dim opt_btn as OptionButton
Set opt_btn = Me.Controls.Add("Forms.OptionButton.1", "optid" & i)

(where i changes each time in the loop, 1,2,3..)

How do I do to add a clickevent on these optionbuttons ?
If the OptionButton was "ordinary" i would write:

Private Sub optid1_Click()
msgbox "This is optid1"
End Sub

But how do I do when they are created dynamic ?? Anyone who knows ? Thanks.
 
Add the WithEvents keyword to your dim statement. You'll find that your variable is added into the list of available objects.

HTH

Bob
 
Alternatively, you can keep one physical instance of the control in the form, make it the first element of an array and use each one's index to identify them in the click event.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
BobRodes-> In the "With" events i can set different properties but can i really call a function from a click ?
.Click ??

vbSun-> Do you have any example to show me ?

Thanks for your help.
 
Erik,

Enjoying in my native at the moment. Please wait till tomorrow till I come back with an example.


------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Here we go as promised

Form1
Controls
Option Button
Properties to change
Index : 0

Command Button
Properties to change
None

Code inside Form1
Code:
Private Sub Command1_Click()

Dim i As Integer

    For i = 1 To 4
        Load Option1(i)
        Option1(i).Left = Option1(i - 1).Left
        Option1(i).Top = Option1(i - 1).Top + Option1(i - 1).Height
        Option1(i).Visible = True
        Option1(i).Caption = "My option Button #" & i
    Next
    
    Option1(0).Caption = "My option Button #0"
    Option1(0).Value = True
    
End Sub


------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Thanks vbSun for your help.
When I try your example I get "type mismatch" on "Load Option1(i)". I did following:

1. Created a userform and added a commandbutton and a optionbutton
2. Added your code behind the commandbuttons click event
3. I cant find a "Index"-property under the optionbutton ??

When I then run it a get type mismatch.

And where do you call the onclick on the optionbutton ?
I want a msgbox to come up when you click the optionbutton that gives you the name of the optionbutton.

Thank you again..
 
This is a VB 6 group, I guess you are using VBA since you refear to your Form as a UserForm (VBA terminology). In VBA you do not have control arrays, sorry.

vbSun's code will work in VB, but not in VBA

---- Andy
 
So will mine. I have no idea if it works in VBA, and I don't understand your question. Sorry!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top