C # determine the control from which the right-click menu (contextmenustrip) pops up

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.