简体   繁体   English

验证是否从其他控件触发了事件?

[英]Validating event fired from other controls?

I have a CheckBox as part of a custom control on a form. 我有一个CheckBox作为表单上的自定义控件的一部分。 I handle its Validating event to ensure that a maximum of 5 checkboxes have been ticked. 我处理其Validating事件,以确保最多选中5个复选框。 If 5 have already been ticked, I cancel the event. 如果5已经被勾选,我取消活动。 This works splendidly. 这非常出色。

Now, however, once I have 5 checkboxes ticked, every event is canceled (except unchecking the first checked CheckBox). 但是,现在,一旦我勾选了5个复选框, 每个事件都将被取消(取消选中第一个选中的CheckBox除外)。 This means that button presses, textbox focuses, combo dropdown and application exits are not working. 这意味着按钮按下,文本框焦点,组合下拉菜单和应用程序退出均不起作用。 The sender object is still a CheckBox; 发件人对象仍然是CheckBox; just not sure why it's being triggered. 只是不确定为什么会被触发。

Why is a checkbox's Validating event being triggered every time I do anything ? 为什么每次执行任何操作都会触发复选框的Validating事件? (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. 其他奇怪的事情:尽管有事件处理程序,但我选择或取消选择第5个复选框时,没有处理任何事件。 The same checkbox will trigger the validating event if it is the 4th or 6th checkbox. 如果同一复选框是第4或第6复选框,则将触发验证事件。

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. MyCustomClass的Selected方法仅返回chkBox.Checked。

How are you validating the input of a CheckBox? 您如何验证CheckBox的输入? It is either true or false . 这是true还是false Are you comparing that check to other states to determine if the current check state is valid? 您是否正在将该检查与其他状态进行比较以确定当前检查状态是否有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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