views:

478

answers:

3

When a winform first displays, the checkbox is unchecked by default. If when the form first displays, I click on the checkbox to 'check' it, the checkbox appears checked for a split second and then disappears. The checkedchanged event never fires. However, if anytime after the first initial attempt I click on the checkbox, the value changes (checked to unchecked and vice versa) like it should and the event fires.

Any idea why the checkbox would not check on the first attempt? It appears selected the first time when you hover over it so I know it has focus.

Update: it doesn't matter if you enter data into all other controls first and then click on the checkbox, the first time you click on it, it flashes as checked for a second, and then the check disappears. Anytime after the 1st time though it works. Strange...

Thnks

+1  A: 

Hard to tell without seeing a code snippet. When I've had things like this in the past, it's been due to having duplicate control ids, or wiring up event handlers incorrectly. Have you tried disabling portions of your code and seeing what affects the checkbox behaviour?

Jeff Paquette
+1 for pointing out that he needs to *reduce his problem problem space.* However, your comment about control IDs smacks heavily of a C++/Win32 background. I would be surprised if one could have duplicate control IDs in C#... The poorly wired event handlers on the other hand is where I'm putting my money...(I just wrote a sample app which only had a checkbox on the form... it stayed clicked the first time I clicked it...)
Jason D
A: 

do this happen with just one CB? or all the CB on the form.

Have you tried deleting the CB then adding it back?

I would suggest you post the code behind the CB?

Crash893
Thanks for responding Crash and flyfishr, I deleted the control and put a new cone on the form with no custom code as a test and I do not get the same behavior. It is difficult to see what if anything is causing this though. When I remove all of the custom code, it still occurs. I have tried setting the focus in the constructor and that sets the control to a checkedstate = checked (which is the opposite of the default behavior I want on form load) but the first click does set it to not checked. If I set focus() and then set the state to unchecked, it does the whole flash and gone thing.
TCH
I have also put the below code in the form constructor as a test but it does not change the behavior:this.chkbox1.UnCheckedValue = "0"; this.chkbox1.CheckedValue = "1"; this.chkbox1.ThreeState = false; this.chkbox1.Value = "0";this.chkbox1.Checked = false;If I break in the click on the first time the control is clicked it goes into the click but it doesn't fire CheckChanged(). It does on subsequent clicks though.
TCH
A: 

Strangely, putting code in the CheckedChanged() to set the value (what it gets set to anyway if I trace through it) seems to work:

        if (this.chkbox1.Checked == true)
        {
            this.chkbox1.Value = "1";
            this.chkbox1.Text = "Checked";
        }
        else
        {
            this.chkbox1.Value = "0";
            this.chkbox1.Text = "Un-checked";
        }

I also put a focus() in the click():

        if (((System.Windows.Forms.MouseEventArgs)(e)).Clicks <= 1)
        {
            if (this.chkbox1.Focused == false)
            {
                this.chkbox1.Focus();
            }
        }

I have no idea why that fixes the problem, but it does.

TCH