lundi 24 janvier 2022

Custom ThreeState CheckBox not working with Click trigger event

I made my own MyCheckBox control by overriding the default CheckBox control. Reason being I wanted to change order of it's states when it's ThreeState property is set to true.

The Default order is

UnChecked -> Checked -> Indeterminate -> Unchecked -> ..... (Repeats)

I wanted the order to be like

UnChecked -> Indeterminate -> Checked -> Unchecked -> ..... (Repeats)

So following is the code which made it happen.

    public class MyCheckBox : CheckBox
    {
        protected override void OnClick(EventArgs e)
        {
            if (AutoCheck)
            {
                if (CheckState == CheckState.Indeterminate)
                {
                    CheckState = CheckState.Checked;
                }
                else if (CheckState == CheckState.Checked)
                {
                    CheckState = CheckState.Unchecked;
                }
                else
                {
                    CheckState = ThreeState ? CheckState.Indeterminate : CheckState.Checked;
                }
            }
        }
    }

All worked fine until here and the problem began when I enabled it's Click trigger event.

enter image description here

The Click event is not firing.

I went back to the CheckBox OnClick() to see if I am missing something. This is the code that is there which I overridden with the above mentioned code.

        protected override void OnClick(EventArgs e)
        {
            if (autoCheck)
            {
                switch (CheckState)
                {
                    case CheckState.Unchecked:
                        CheckState = CheckState.Checked;
                        break;
                    case CheckState.Checked:
                        if (threeState)
                        {
                            CheckState = CheckState.Indeterminate;
                            if (AccObjDoDefaultAction)
                            {
                                AccessibilityNotifyClients(AccessibleEvents.StateChange, -1);
                            }
                        }
                        else
                        {
                            CheckState = CheckState.Unchecked;
                        }

                        break;
                    default:
                        CheckState = CheckState.Unchecked;
                        break;
                }
            }

            base.OnClick(e);
        }

I missed the base.OnClick(e) method of course but as soon as I add this method to my MyCheckBox OnClick(), now the Click event is firing okay but the CheckState of MyCheckBox is stuck at Unchecked. No matter how many times I click it's stuck on Unchecked and doesn't change.

I spent hours figuring out what is happening but with no luck.

Upon further debugging I found out that the CheckState does change (Can see it if I pause the program just before the Click trigger event fires) but after it's Click event ends the CheckState reverts back to UnChecked.

Can anyone figure this out? I give up.

I am on .net winforms.




Aucun commentaire:

Enregistrer un commentaire