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.Ca开发者_Python百科ncelEventHandler(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.
public partial class Form1 : Form {
private void pnlClickOnMe_MouseClick(object sender, MouseEventArgs e) {
Point mouseLocation = MousePosition;
// get point of mouse relative to pnlClickOnMe
Point pointYouAreInterestedIn = pnlClickOnMe.PointToClient(mouseLocation);
lblShowCoordinates.Text = string.Format("{0} - {1}", pointYouAreInterestedIn.X, pointYouAreInterestedIn.Y);
}
public Form1() {
InitializeComponent();
}
#region designer
private void InitializeComponent() {
this.lblShowCoordinates = new System.Windows.Forms.Label();
this.pnlClickOnMe = new System.Windows.Forms.Panel();
this.SuspendLayout();
this.lblShowCoordinates.AutoSize = true;
this.lblShowCoordinates.Location = new System.Drawing.Point(32, 18);
this.lblShowCoordinates.Size = new System.Drawing.Size(35, 13);
this.pnlClickOnMe.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.pnlClickOnMe.Location = new System.Drawing.Point(31, 64);
this.pnlClickOnMe.Size = new System.Drawing.Size(206, 152);
this.pnlClickOnMe.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pnlClickOnMe_MouseClick);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.pnlClickOnMe);
this.Controls.Add(this.lblShowCoordinates);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblShowCoordinates;
private System.Windows.Forms.Panel pnlClickOnMe;
}
Did you use the designer to build your forms ?
It's pretty easy to do what you need, without any coding effort.
Look at this link for more info:
http://msdn.microsoft.com/en-us/library/ms173080%28VS.80%29.aspx
Anyway, about context menus:
1.
You don't need to find mouse position and open the menu manually.
Just set the ContextMenuStrip
property of your picturebox to your context menu like:
pictureBox.ContextMenuStrip = yourContextMenuInstance;
// you can do this also from Designer
and when you right-click on a pixel in the pictureBox, automatically your menu will appear in the right position (I mean in the mouseposition).
2.
If your menu items don't depend on menu position or some other changeable status, you can add them at menu creation and not in the Opening
event handler.
The same is for the ItemClick
handlers, you can attach them at items creation.
3.
Maybe you don't need this feature, but you can add submenus to any item in your context menu.
So, to resume:
in your Form1
you need to create (by designer, or code as you wish) a PictureBox
(let's call it pbox1
), a ContextMenu
(let's call it menu1
) and your aaa
item, with its corresponding event handler.
Hence, set pbox1.ContextMenuStrip = menu1
and in the aaa click event handler you can open you Form2
.
Then, you can previously define you Form2
, in the same manner of Form1
, i.e. with its contextMenu attached to the textbox etc...
精彩评论