[dba-VB] CheckBox: Checked and CheckState

Gustav Brock Gustav at cactus.dk
Fri May 9 06:22:33 CDT 2008


Hi all

Here's a simple dirty trick to force a validate of a CheckBox: 
Hide the checkbox, Show it again and set Focus. 

As the control looses focus, Validating and Validated will fire. The previous control will get focus. You show the control again and move focus back (and the Validating and Validated events of the previous control will fire). It happens so fast that nothing strange is to be seen for the user.

        private void checkBoxRegistered_CheckedChanged(object sender, EventArgs e)
        {
            this.checkBoxRegistered.Hide();
            // Focus has left the control. 
            // Validating and Validated will fire.
            this.checkBoxRegistered.Show();
            this.checkBoxRegistered.Focus();
            // Focus has been retained.
            // Validating and Validated of the previous control will fire.
        }

Another option is to have a tiny dummy control containing no event code where focus temporarily can be stoved away:

        private void checkBoxRegistered_CheckedChanged(object sender, EventArgs e)
        {
            this.noop.Focus();
            // Focus has left the control. 
            // Validating and Validated will fire.
            this.checkBoxRegistered.Focus();
            // Focus has been regained.
            // Validating and Validated of the dummy control will fire.
        }

/gustav


>>> Gustav at cactus.dk 09-05-2008 10:50 >>>
Hi Charlotte

> it's easier to use the validating event and check for Checked. 

That was one of my attempts but problem is that Validating (and Validated) doesn't fire until focus is moved to another validating control.
I have a set of checkboxes, and for some of these Enabled has to be turned on and off according to the values of the other checkboxes. This should happen when you click (or press Space) any enabled checkbox. So that leaves me with Checked and CheckedState

> the checked property changes before the value does.

Aren't they the same? The Checked property represents the value according to IntelliSense. And - as I see it - this is set before CheckState.

What I wish to understand is the difference between these. So I located here:

  http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1198715&SiteID=1 

this which seems to tell that it has to do with the painting of the control (hold on):

<quote>
When you set the Checked property it actually sets the CheckedState property which will cause the CheckedChanged and CheckStateChanged events to be raised.  When this happens the Windows message is sent (which should update the UI).  However if you have enabled owner draw then Windows won't repaint anything.  When CheckStateChanged is raised it will call Refresh if the control is owner draw.  However if you circumvented CheckedChange then CheckStateChanged won't get raised either it appears.  Therefore owner draw boxes won't paint.  Are you using owner draw?  If so then call Refresh in your OnCheckedChanged override to force a repaint (although you might want to do a  if-owner draw statement first).
</quote>

I had to read this twice, mostly because I don't bother with the painting, just uses the default behaviour.

What really puzzles me is that you can set either Checked or CheckState but no matter which one you set, _both_ of these are set ... and always in the same sequence: Checked, then CheckState.

/gustav


>>> cfoust at infostatsystems.com 09-05-2008 00:47 >>>
If the checkboxes are bound, it's easier to use the validating event and
check for Checked.  Checkbox values get a little strange and the checked
property changes before the value does.

Charlotte Foust 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Thursday, May 08, 2008 3:14 PM
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] CheckBox: Checked and CheckState

Hi Doris

So that's it? CheckedChanged happens "when" the state changes,
CheckStateChanged after?
Does this relate to the data source it is bound to? Like BeforeUpdate
and AfterUpdate in Access?

The EventArgs methods seem identical, however, for the two events.

/gustav

>>> mikedorism at verizon.net 08-05-2008 23:43 >>>
You have to change the checkbox value (CheckedChanged event) before you
can examine the CheckStateChange -- kind of like you have to process the
Validating event before the Validated event.

Doris Manning
Database Administrator
Hargrove Inc.

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Thursday, May 08, 2008 12:30 PM
To: dba-vb at databaseadvisors.com 
Subject: [dba-VB] CheckBox: Checked and CheckState

Hi all

What is the difference between these two events for a bound CheckBox?

Whatever I try, first event CheckedChanged is fired, then
CheckStateChanged
- no matter if I browse the source it is bound to or if I click or
keypress the CheckBox itself (has focus).

/gustav




More information about the dba-VB mailing list