简体   繁体   中英

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.

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. 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.

In my pre C# windows programming experience, clicking on the disabled button would have had no affect on the running program. 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)
            }
        };

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