I am writing a VB app which has a form with 3 option buttons on it. In the Form Load I am setting one of the option button's Value property based on information read from the registry. When I ran it, I was surprised that this triggered the On Click event for that option button. I didn't expect it to.
How do I tell between that and a User clicking the option button without setting some global variable to control things?
I wouldn't expect it to either. I've just tried a basic example and i didn't get the same result as you. Can you post the code on how you are doing this.
I have attached the project as Test.zip. It is very small test, presumably the same as you have done. You will see that a message box is displayed as you start the project, showing that an Option3_Click event was generated when its value was set to True in Form_Load.
Are you sure you have your primary platform set correctly? Only becuase it was set to .net 2003 i assumed you were talking about a radiobutton, but i'm starting to get the feeling that you are using 2005 or somekind of activex control?
It sounded strange to me too so I tested it. I'm also assuming that "option button" means RadioButton. Can I recommend that you use the proper names of controls in future so that there is no confusion. In both 2003 and 2005 I added two RadioButtons to a form and then added this code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.RadioButton2.Checked = True
End Sub
Private Sub RadioButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.Click
MessageBox.Show("RadioButton2.Click")
End Sub
Sure enough I received the message when I ran the project. Seems pretty silly but if that's the way it is then just use a boolean variable to indicate whether the form has been initialised or not.
The app I attached in my previous reply was written in VB6, but I also tried it in VB 2005 and the same thing happens only more so. Not only does it trigger the CheckedChange event, which is the default and understandable, but it still triggers the Click event, which is not.
As you say, the way round is a boolean to control things, but I don't think it should be necessary.
Sorry about the naming confusion. In VB6 they are Option buttons, having been radio buttons in earlier versions, to which they have now returned in later versions.
I don't think it should be necessary either but if there's a bug present then you have to work around it. You should also make a bug report to Microsoft, although I wouldn't expect this to be high on their list of priorities. You can't complain about it if you don't do your bit to get it fixed though.
You get this behaviour when a radio button is checked = true and one of the radio buttons in the group is focused when the form becomes visible
-
Make a new project, one form and on it place One button and Two radiobuttons
Assign the tab order so it is the button first, then either of the radio buttons
In each radio button's click event, write code that will change the relevant radiobutton's text to indicate it was clicked
In the form load, set checked=true on one radio button
Start the project.
The form will appear, with the focus-dotted-rectangle nested on the button, one radio button is checked, the clicked event does not fire
-
Stop the prject, change the tab order so that either of the radio buttons is tab index 0
Restart the project.
Note the focus rectangle now nestles on the radiobutton with the checked=true and the click event has fired for that rb
-
Stop the project, Reassign tab order so the other radio button is first, restart the project.
The behaviour is same as above.
-
Stop the project, in the form load assign both RB checked to true sequentially and restart the project.
The form comes to visible with the most recently checked RB showing checked, and only its click event fired
Hence, it matters not which RB is tabindex 0, so long as one RB in the group is tabindex 0. If it is, then whichever RB you most recently set to true before the form became visible, will have its click event fired just after the form becomes visible (it might not be showing on screen at that point i.e. you wont see the focus change, but Me.Visible is true).
To negate the behaviour, either ensure that a non-radiobutton control is the focus (tabindex 0) when the form becomes visible
here is the example code i used in investigating the condiiton:
ps, i beleive this affects other controls (at least, i seem to recall it doing so in vb6) like listbox, and maybe checkboxes..
pps; before a purist observation is made.. when i say "ensure that a non-radiobutton control is the focus (tabindex 0)" i implicitly mean for you to set a control that can actually receive the focus.. it's no good setting a label to tabindex 0, or setting a control whose tabstop property is false, to be index 0. hence, maybe i should say "ensure that a non-radiobutton control is the first focusable control (according to tabindex order and tabstop values) "
given that [i'm sure] it has existed since vb6, i'm surprised that it is not more widely known.. it may be by design.. if you hear anything back, be sure to let us know!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.