Reputation: 1237
I want to be able to right click on an image and for there to be a menu show up. When I then click on one of the items I want it to point to a class:
private void link1add_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Add", HandleContextMenuAdd);
cm.MenuItems.Add("Remove", HandleContextMenuRemove);
link1add.ContextMenu = cm;
}
}
private void HandleContextMenuAdd(object sender, EventArgs e)
{
MessageBox.Show("Adding");
}
private void HandleContextMenuRemove(object sender, EventArgs e)
{
MessageBox.Show("Removing");
}
Code edited since first posted. Thanks all for your help.
Upvotes: 2
Views: 944
Reputation: 6026
Pattern for your own code after this:
public class ReadDocumentEventArgs : EventArgs
{
public string ImageInfo { get; set; }
}
public void ReadDocument(object sender, ReadDocumentEventArgs ea)
{
// do whatever you need to do
MessageBox.Show(ea.ImageInfo); // example
}
private void link1add_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
PictureBox imageCtrl = sender as PictureBox;
// get the information you need to get from your control to identify it
string imgInfo = "Hello, World!"; // example
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Item 1");
cm.MenuItems.Add("Item 2",
(EventHandler)((s, rde) =>
{ ReadDocument(s, new ReadDocumentEventArgs()
{ ImageInfo = imgInfo });
}));
link1add.ContextMenu = cm;
}
}
In your MouseDown over your image you can create a menu item using the code I supplied above that will call an event handler called ReadDocument. Notice that there is a ReadDocumentEventArgs class that you can customize to contain the necessary properties that will help you identify which image you have clicked on.
So, in the example I have above one of the first things that happens in MouseDown is that you get the instance of image control (I assume it's a Picture Box, but you can cast it to whatever it really is).
At that point, you can then get a file name or whatever from your image that identifies it from the other controls on your form.
Next, when creating a context menu item, it tells the menu item to call ReadDocument but passes in the special data just taken from the image control.
In the ReadDocument method, you can then do whatever you need to do. In my example, I simply throw up a MessageBox to show you what data you passed in.
Upvotes: 1
Reputation: 4232
What about a lambda expression?
cm.MenuItems.Add("Item 2", (_, __) => {
if (...) ReadDocument();
});
or
cm.MenuItems.Add("Item 2", (_, __) => { this.myClassInstance.DoSomething(); });
Alternatively, you can create a method with the signature of the expected event handler:
cm.MenuItems.Add("Item 2", HandleContextMenuClick);
private void HandleContextMenuClick(object sender, EventArgs e)
{
if (...) ReadDocument();
}
That method doesn't need to be in the same class (you can write this.myClassInstance.HandleContextMenuclick
for example). But I would hide the implementation detail from other classes to avoid unnecessary coupling.
Upvotes: 1
Reputation: 190915
What you will want is to put another event handler and call your readdocument
method.
Upvotes: 0