changing the value of a checkbox fires it's click event.
Geek-9pm: Actually, I think I can answer that question: for .NET the prototype for the "click" even of a checkbox is something like:
Private Sub CheckBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
the prototype they gave matches the Pre .NET versions of VB though- so it's probably VB5 or 6. So, basically; changing the Value property of a checkbox will fire it's "click" event. So the question is; how to overcome this?
If it isn't causing any problems, don't worry about it. Generally the click event of a checkbox should simply be inspecting the new value and changing other values as appropriate. If your changing the database from the checkbox directly then you should reconsider your design scheme; a database-driven application really should allow for transactional operations; for example, use an "apply" button instead of directly changing the database values.
If however the click even is merely an annoyance and/or for some reason is causing other issues, it's easy to circumvent the default Visual Basic handling of the event.
Alright, so maybe easy isn't the perfect description.
A Checkbox is, internally, the same window class as a button (
http://msdn.microsoft.com/en-us/library/bb775947%28VS.85%29.aspx). It's really just a type of button. Therefore, it will respond to the various button messages if we send it directly to the checkbox.
The important Message here is BM_SETCHECK, documented here:
http://msdn.microsoft.com/en-us/library/bb775989%28VS.85%29.aspxBut, ALAS! using the sendmessage API to set the state STILL fires the click event! Oh the horror!
The only feasible way to overcome this would be to literally subclass the form and throw away the right notifications from the checkbox (which visual basic would otherwise use to fire events). the bookkeeping involved is enormous, and subclassing for something like this is a bit overboard.
The solution? Well, if your populating and setting the checkbox properties in Form load, you can use a form level variable to determine, in your checkbox click events, wether they should execute. For example:
Option Explicit
Private mIgnoreCheckClick as Boolean
Private Sub Form_Load()
'database population, etc...
mIgnoreCheckClick=True
'assign values to checkboxes
mIgnoreCheckClick=False
End Sub
Private Sub Check1_Click()
If mignorecheckclick then exit sub
'normal checkbox code.
End Sub
'etc