In the previous articleArticleIn the C # formProgramRight-click the control and add different events to different sub-menus: C # How to right-click a widget and add different events to different submenus In this article, Xiaoyao will introduce C #'s method of judging the control from which the right-click menu (contextmenustrip) pops up. When I use C # as an online classroom program for my whole life, I need to put it on the form This article comes from the life of happy: http://www.xiaoyaolife.com/ The two panel controls have a label, label1, 192.168.1.2, label2, and 192.168.1.3 respectively, representing the two clients of the client, Both labels are bound to a control called contextmenustrip1. There are two menus: shutdown and restart. Because the two icons represent two different clients, the right-click menu has two sub-menus: shutdown and restart. Obviously, right-click the client panel, the pop-up menu and command execution are the same. How do I know which client is shut down after I click shutdown?This article comes from the life of happy: http://www.xiaoyaolife.com/ Double-click contextmenustrip1 to write the followingCode: Private void contextmenustripappsopening (Object sender, canceleventargs E) { String whichcontrol_name = (senderAsContextmenustrip). sourcecontrol. Name; MessageBox. Show (whichcontrol_name ); } Running result: Right-click label2 in panel2 Some friends say what is your use and get the IP value in the label, This article comes from the life of happy: http://www.xiaoyaolife.com/ See the following method: 1, Private void contextmenustripappsopening (Object sender, canceleventargs E) { Panel whichcontrol_name = (panel) (sender as contextmenustrip). sourcecontrol; Label L = (Label) whichcontrol_name.controls [0]; MessageBox. Show (L. Text ); } Or This article comes from the life of happy: http://www.xiaoyaolife.com/ 2, Private void form1_load (Object sender, eventargs E) { Panel1.name = "192.168.1.2 "; Panel2.name = "192.168.1.3 "; } Private void contextmenustripappsopening (Object sender, canceleventargs E) { String whichcontrol_name = (sender as contextmenustrip). sourcecontrol. Name; MessageBox. Show (whichcontrol_name ); } This time, I right-click label1 in Panel1. The result is: Of course, the panel on the client may be dynamically added. You can give it a name when adding the panel. This article comes from the life of happy: http://www.xiaoyaolife.com/ We can define a global variable. String IP = NULL; Private void contextmenustripappsopening (Object sender, canceleventargs E) { String whichcontrol_name = (sender as contextmenustrip). sourcecontrol. Name; IP = whichcontrol_name; } In this way, you can call this IP value globally, that is, you know which client you are operating on. |