OptionButton Click Event Fire on Form Load?
I came across a weird issue I've never seen before. I have a form with two option buttons (aka radio buttons) in a control array. When the form loads, the click event fired, even though the Value property isn't set anywhere in the code.
As a test, I changed it from a control array into two separately named controls, and now the click event doesn't fire.
This solves my problem, but I thought it was a weird issue. Do control arrays reinitialize at load even when they are set at design time?
Just curious...
- Nick
Re: OptionButton Click Event Fire on Form Load?
No it doesn't get fired automatically. There must be something in the code which is triggering it... Test it for yourself... start a new project and create an array of opt button and in the click event type this
Code:
Private Sub Form_Load()
'~~> Nothing in form load
End Sub
Private Sub Option1_Click(Index As Integer)
MsgBox "Hello"
End Sub
You will see that the mesage box doesn't show on form load...
Re: OptionButton Click Event Fire on Form Load?
It returns True because if you have an array of OptionButtons one of them must be true.
Code:
Option Explicit
Private Sub Form_Load()
Me.Show
If Option1(0).Value = True Then
MsgBox "Option1(0).Value = True"
End If
If Option1(1).Value = False Then
MsgBox "Option1(1).Value = False"
End If
Print Option1(0).Value
Print Option1(1).Value
End Sub
Re: OptionButton Click Event Fire on Form Load?
Quote:
Originally Posted by koolsid
No it doesn't get fired automatically. There must be something in the code which is triggering it
The default option is set at design time, and no part of the code ever changes the option value. The event only changed some other properties based on the Index value. As I said, this is a weird one.
-- Nick
Re: OptionButton Click Event Fire on Form Load?
@CDrive : But you are checking for the value in the form load... OP has a different query...
Quote:
When the form loads, the click event fired, even though the Value property isn't set anywhere in the code.
Re: OptionButton Click Event Fire on Form Load?
Nick did you try the experiment that i mentioned above?
Re: OptionButton Click Event Fire on Form Load?
For reference my code looked like this:
Code:
Private Sub optSetRange_Click(Index As Integer)
Dim sSource As String
Dim bEnabled As Boolean
sSource = "optSetRange_Click"
Debug.Print "Call stack - " & sSource
bEnabled = (Index = 1)
'Set Enabled property of several controls based on bEnabled
End Sub
I had inserted the Debug.Print statements in every Sub and Function in the form's code to trace another issue when I noticed that this event was being triggered at load, without the option being clicked.
The only thing I can think of is that the line,
Code:
bEnabled = (Index = 1)
was assigning the Index value instead of testing equality, and thereby re-triggering the event. If that were the case, though, I would expected the event to re-fire until the stack overflowed when clicked manually. I tested that, and it doesn't happen.
Like I said, splitting the control array into separately named option buttons with their own click events resolved the issue, so I'm not going to dwell on it. I just thought it was odd.
@koolsid - No I didn't try your experiment. I'm knee deep in the current code, so I'll take your word for it.
Re: OptionButton Click Event Fire on Form Load?
If you are interested, we can check if optSetRange_Click(Index As Integer) is being fired or not... simply replace the above code by a messagebox
Private Sub optSetRange_Click(Index As Integer)
msgbox "fired"
End Sub
If you see the message box at form load then definitely your above mentioned code is not responsible....
Re: OptionButton Click Event Fire on Form Load?
Here is the scenario which is probably what applies to you.
1. If an option button is unselected and it gets the focus, it will select itself and fire the click event, regardless whether it is arrayed or not.
2. You are getting the click event on form load because the option button receiving the event has a tab index of 0 (the first control to gain focus on the form) and had a value of unselected.
Re: OptionButton Click Event Fire on Form Load?
Quote:
Originally Posted by LaVolpe
2. You are getting the click event on form load because the option button receiving the event has a tab index of 0 (the first control to gain focus on the form) and had a value of unselected.
That sounds like the most likely cause, since one of the option buttons is tab index 0.
Re: OptionButton Click Event Fire on Form Load?
Quote:
Originally Posted by LaVolpe
Here is the scenario which is probably what applies to you.
1. If an option button is unselected and it gets the focus, it will select itself and fire the click event, regardless whether it is arrayed or not.
2. You are getting the click event on form load because the option button receiving the event has a tab index of 0 (the first control to gain focus on the form) and had a value of unselected.
This is interesting because either one of these statements in the Form_Load event will fire the Option1_Click() event, even though both of them are already true by default.
Code:
Option Explicit
Private Sub Form_Load()
Option1(0).TabIndex = 0
' Option1(0).Value = True
End Sub
Private Sub Option1_Click(Index As Integer)
If Option1(0) Then MsgBox "Option1(0) Clicked"
If Option1(1) Then MsgBox "Option1(1) Clicked"
End Sub
Re: OptionButton Click Event Fire on Form Load?
CDrive, interesting indeed. If all I have on a form are the 2 option buttons, arrayed as you do, and Option(0) has both TabIndex=0 & Value=True, I do not get any click events in either scenario during form_load, as you are describing. Now if none had Value=True, then I would.
P.S. Are you sure that Option1(1) was not TabIndex=0, in design view, during your tests?
Re: OptionButton Click Event Fire on Form Load?
LaVolpe, at Design Time Option1(0).TabIndex = 0 and Value = False, but this fact brings me back to my statement in post#3. I think the point here is that more than just Clicking on an OptionButton can fire the Click event.