简体   繁体   中英

How to check which control is clicked with one click event handler

What I'm trying to do is find out which control is clicked in my application without call MouseClick handler multi times for every control.
Assume that we have a form which contains some controls and then one of them is clicked by user and a Messagebox pups up and shows which control is clicked.
In other word there is just one handler with a sender argument so that sender tell me which control is clicked.
Something like below code:

private void MouseClicked(object sender, EventHandler e)
    {
        if(sender is Button)
        {
            //do something 
        }
        if (sender is Label)
        {
            //do something else
        }
        /*
         * .
         * .
         * .
         * 
        */
    }

Simply select all the controls from the designer of the form and go to the events tab.

Double click on the mouse click event and one handler would be generated for those events.

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