简体   繁体   English

单击禁用的控件将在C#中调用父事件

[英]Clicking on a disabled control is calling the parent event in C#

I'm using VisualStudio 2013 with C# and I'm fairly new to this environment and language, so please bear with me if this is a silly question. 我正在将VisualStudio 2013与C#一起使用,并且对这种环境和语言还很陌生,所以如果这是一个愚蠢的问题,请多多包涵。

I have a set of buttons that reside on a panel. 我有一组位于面板上的按钮。 All of the buttons have onClick events, as well as the panel that the buttons sit on. 所有按钮都具有onClick事件以及按钮所在的面板。 If the button is enabled then the buttons event handler is called as expected. 如果启用了按钮,则按预期调用按钮事件处理程序。 However; 然而; if the button is not enabled, then the panel's onclick event handler is called. 如果未启用该按钮,则将调用面板的onclick事件处理程序。

In my pre C# windows programming experience, clicking on the disabled button would have had no affect on the running program. 在我以前的C#Windows编程经验中,单击禁用按钮对运行的程序没有影响。 What's going on here, and how do I keep the parents events from being triggered? 这是怎么回事,如何避免触发父母事件?

You could detect it by something like: 您可以通过以下方式检测到它:

        panel.Click += delegate {
            var l = Cursor.Position;
            l = panel.PointToClient(l);
            var c = panel.GetChildAtPoint(l);
            if (c == null) {
                // good
            }
            else {
                // ignore (disabled control)
            }
        };

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

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