简体   繁体   中英

Validating event fired from other controls?

I have a CheckBox as part of a custom control on a form. I handle its Validating event to ensure that a maximum of 5 checkboxes have been ticked. If 5 have already been ticked, I cancel the event. This works splendidly.

Now, however, once I have 5 checkboxes ticked, every event is canceled (except unchecking the first checked CheckBox). This means that button presses, textbox focuses, combo dropdown and application exits are not working. The sender object is still a CheckBox; just not sure why it's being triggered.

Why is a checkbox's Validating event being triggered every time I do anything ? (I would have thought it would only have been triggered when you click the checkbox control). Is using the "Validating" event the correct approach?

Additional strange things: No event is being handled when I select or deselect the 5th checkbox, despite having the event handler there. The same checkbox will trigger the validating event if it is the 4th or 6th checkbox.

My Code: Within the parent form:

    private List<MyCustomClass> GetSelectedItems()
    {
        List<MyCustomClass> result = new List<MyCustomClass>();
        foreach (MyCustomClass c in listOfControls)
        {
            if (c.Selected())
            {
                result.Add(c);
            }
        }
        return result;
    }

    private void validate(object sender, CancelEventArgs e)
    {
        if (GetSelectedItems().Count == 5)
        {
             e.Cancel = true;
        }
    }

The Selected method of MyCustomClass simply returns chkBox.Checked.

How are you validating the input of a CheckBox? It is either true or false . Are you comparing that check to other states to determine if the current check state is valid?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM