Computer Hope

Software => Computer programming => Topic started by: linguist2000 on February 04, 2010, 11:30:27 AM

Title: confused with checkboxess need help
Post by: linguist2000 on February 04, 2010, 11:30:27 AM
I have listview, checkbox on form, and populated them data from database.

what is problem is that:

while populating checkboxex all subs related with are running.

for example

form_load runs at start
but

Private sub check1_click()
[COLOR="Red"]HERE ALSO RUNS without my permission at start up. without clicking checkbox[/COLOR]
end sub

how can I prevent this? Does anyone know? Thanks
Title: Re: confused with checkboxess need help
Post by: Geek-9pm on February 04, 2010, 02:27:34 PM
It would be helpful it you gave some more details.  ???
Are you using This?

.NET Framework Class Library ListView Class
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx
And which version of Visual Basic?
Title: Re: confused with checkboxess need help
Post by: linguist2000 on February 04, 2010, 02:49:36 PM
VB 6

I just set the checkbox's value from database in the form_load sub

can it be because of this?
Title: Re: confused with checkboxess need help
Post by: BC_Programmer on February 04, 2010, 03:05:33 PM
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:

Code: [Select]
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.aspx

But, 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:

Code: [Select]

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
Title: Re: confused with checkboxess need help
Post by: linguist2000 on February 04, 2010, 03:16:01 PM
I just tried but I have many sub procedures and it fails
Title: Re: confused with checkboxess need help
Post by: BC_Programmer on February 04, 2010, 03:17:41 PM
I just tried but I have many sub procedures and it fails

be more specific.