By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Filmstrip control, adding a contextual menu to items

P: 36
I have a filmstrip control on my windows C# app. It's populated with images. Is there anyway to add a right-click contextual menu to each image? I want the user to have the ability to right click and have some options (like open image in a graphics application).

Thanks for any help.
Feb 3 '09 #1
Share this Question
Share on Google+
7 Replies


vekipeki
Expert 100+
P: 229
You can handle the Control.MouseUp event to display the menu:

Expand|Select|Wrap|Line Numbers
  1. private void yourControl_MouseUp
  2.     (object sender, MouseEventArgs e)
  3. {
  4.     if (e.Button == MouseButtons.Right)
  5.     {
  6.         System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
  7.         contextMenu.Items.Add("some menu item");
  8.         contextMenu.Show(sender as Control, e.X, e.Y);
  9.     }
  10. }
Feb 3 '09 #2

P: 36
Ok, my control is named filmStrip2.

So here is what I did:

Expand|Select|Wrap|Line Numbers
  1. private void filmStrip2_MouseUp(object sender, MouseEventArgs e)
  2.         {
  3.             if (e.Button == MouseButtons.Right)
  4.             {
  5.                 System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
  6.                 contextMenu.Items.Add("some menu item");
  7.                 contextMenu.Show(sender as Control, e.X, e.Y);
  8.             }
  9.         }

And then in Form1.Designer.cs I put:
Expand|Select|Wrap|Line Numbers
  1. this.filmStrip2.MouseUp += new System.Windows.Forms.MouseEventHandler(filmStrip2_MouseUp);
Does this look correct? Because I couldn't get this to work.
Feb 3 '09 #3

P: 36
I also tried this:

Expand|Select|Wrap|Line Numbers
  1. private void filmStrip2_OnImageClicked(object sender, EventArgs e)
  2. {
  3.     MouseEventArgs f = (MouseEventArgs)e;
  4.     if (f.Button == MouseButtons.Right)
  5.     {
  6.         System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
  7.         contextMenu.Items.Add("some menu item");
  8.         contextMenu.Show(sender as Control, f.X, f.Y);
  9.     }
  10. }
Of course in Form1.Designer.cs I have: this.filmStrip2.OnImageClicked += new System.EventHandler(this.filmStrip2_OnImageClicked );

But I get an exception saying that sender is null. It's weird because I can right click and display a message box containing sender.ToString and it displays correct information.
Feb 3 '09 #4

Plater
Expert 5K+
P: 7,872
In your last example I think sender would be an instance of Image, which is not a Control, so that "sender as Control" line would send a "null" parameter
Feb 3 '09 #5

P: 36
Ok, I figured it out. In case anyone searches for this problem, this is what I did:

Expand|Select|Wrap|Line Numbers
  1. private void filmStrip2_OnImageClicked(object sender, EventArgs e)
  2. {
  3.     MouseEventArgs f = (MouseEventArgs)e;
  4.     if (f.Button == MouseButtons.Right)
  5.     {
  6.         string controlIndex = sender.ToString().Substring(sender.ToString().Length-1,1);                
  7.         System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
  8.         contextMenu.Items.Add("some menu item");
  9.         try
  10.         {
  11.             contextMenu.Show(filmStrip2.Controls[0].Controls[Convert.ToInt32(controlIndex) * 2], f.X, f.Y);
  12.         }
  13.         catch { }
  14.     }
  15. }
Please let me know if I am doing anything inefficient. Thanks for your help vekipeki and plater.
Feb 3 '09 #6

vekipeki
Expert 100+
P: 229
Well, the thing that looks a bit suspicious to me is getting the index from sender.ToString(). You are presuming that your sender.ToString() will return something like "Image#", where # is the index in Controls collection?

Are you sure that you index always has only 1 digit? (Sorry if I am writing nonsense, I haven't used this control before)
Feb 4 '09 #7

P: 36
I have it programmed so that the image will always be product-#. But you're right about the # being 1 digit... that was dumb.

This should work: string controlIndex = sender.ToString().Split('-')[1];

Thanks a lot for the insight!
Feb 4 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.