my goal is to open a ContextMenuStrip on right click remember this click x and y and then on clicking on one of the given items to do something till now this is what i have done:
public delegate void mydelegate(string s);
public Form1()
{
InitializeComponent();
m_MyContextMenuStrip = new ContextMenuStrip();
m_MyContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);
this.ContextMenuStrip = m_MyContextMenuStrip;
}
void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
m_MyContextMenuStrip.Items.Clear();
location1 = m_MyContextMenuStrip.PointToClient(new Point(0, 0));
location = m_MyContextMenuStrip.PointToScreen(new Point(0, 0));
// Populate the ContextMenuStrip control with its default items.
m_MyContextMenuStrip.Items.Add("-");
m_MyContextMenuStrip.Items.Add("Apples");
m_MyContextMenuStrip.Items.Add(location.X.ToString() + "and " + location.Y.ToString());
m_MyContextMenuStrip.Items.Add("aaaa", null, new EventHandler(onaaaClick));
// Set Cancel to false.
// It is optimized to true based on empty entry.
e.Cancel = false;
}
private void onaaaClick(object sender, EventArgs e)
{
//will handle the click on aaa
Form2 f2 = new Form2(functodel);
f2.Show();
}
void functodel(string s)
{
Label l = new Label();
l.Text = s;
l.Top = location.Y - 108;
l.Left = location.X - 100;
//l.Left = this.Location.X - location.X;
l.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(l);
l.BringToFront();
}
where Form2 is:
private mydelegate m_del;
public Form2(mydelegate del)
{
m_del = del;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
m_del(textBox1.Text);
this.Close();
}
my problem is when i click on the right click i am not getting the location i want, i am getting another location. can someone explain to me what am i doing wrong ?
-
how to get the right location.
-
how to get a ContextMenuStrip where i had only once an item?
-
let’s say on the given label i am righting i want to click on the right click and get another optiones then in m_ContextMenuStrip (delete item and change color) how do i do that?
Edit:
I would try to explain my motivation better
my goal is a picture where you click on the right click on coordinate(x and y ) openes for you a there a menu(menu1) just like you do on windows desktop the menu top left is the coordinate you have clicked and from this coordinate a menu is opened (just like i want to change or open something on the desktop it doesn’t opened in the left top corner of desktop it’s opened where I have clicked ).
when the aaa item is clicked i want another form to be opened and from there with the delegate open new label form1
now when this label is clicked with right click i want another menu to be opened (let’s call it menu2)
menu1 and menu2 are different creatures
I might have missed the main concept?
You could access the mouse coordinates and calculate that to the point of your picturebox (in this test a panel)?
You would calculate it on the opening event of the menustrip.
And MousePosition is a static property on the Control class.