Filmstrip control, adding a contextual menu to items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • z1freeride
    New Member
    • Feb 2009
    • 36

    Filmstrip control, adding a contextual menu to items

    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.
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    You can handle the Control.MouseUp event to display the menu:

    Code:
    private void yourControl_MouseUp
        (object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
            contextMenu.Items.Add("some menu item");
            contextMenu.Show(sender as Control, e.X, e.Y);
        }
    }

    Comment

    • z1freeride
      New Member
      • Feb 2009
      • 36

      #3
      Ok, my control is named filmStrip2.

      So here is what I did:

      Code:
      private void filmStrip2_MouseUp(object sender, MouseEventArgs e)
              {
                  if (e.Button == MouseButtons.Right)
                  {
                      System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
                      contextMenu.Items.Add("some menu item");
                      contextMenu.Show(sender as Control, e.X, e.Y);
                  }
              }

      And then in Form1.Designer. cs I put:
      Code:
      this.filmStrip2.MouseUp += new System.Windows.Forms.MouseEventHandler(filmStrip2_MouseUp);
      Does this look correct? Because I couldn't get this to work.

      Comment

      • z1freeride
        New Member
        • Feb 2009
        • 36

        #4
        I also tried this:

        Code:
        private void filmStrip2_OnImageClicked(object sender, EventArgs e)
        {
            MouseEventArgs f = (MouseEventArgs)e;
            if (f.Button == MouseButtons.Right)
            {
                System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
                contextMenu.Items.Add("some menu item");
                contextMenu.Show(sender as Control, f.X, f.Y);
            }
        }
        Of course in Form1.Designer. cs I have: this.filmStrip2 .OnImageClicked += new System.EventHan dler(this.filmS trip2_OnImageCl icked);

        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.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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

          Comment

          • z1freeride
            New Member
            • Feb 2009
            • 36

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

            Code:
            private void filmStrip2_OnImageClicked(object sender, EventArgs e)
            {
                MouseEventArgs f = (MouseEventArgs)e;
                if (f.Button == MouseButtons.Right)
                {
                    string controlIndex = sender.ToString().Substring(sender.ToString().Length-1,1);                
                    System.Windows.Forms.ContextMenuStrip contextMenu = new ContextMenuStrip();
                    contextMenu.Items.Add("some menu item");
                    try
                    {
                        contextMenu.Show(filmStrip2.Controls[0].Controls[Convert.ToInt32(controlIndex) * 2], f.X, f.Y);
                    }
                    catch { }
                }
            }
            Please let me know if I am doing anything inefficient. Thanks for your help vekipeki and plater.

            Comment

            • vekipeki
              Recognized Expert New Member
              • Nov 2007
              • 229

              #7
              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)

              Comment

              • z1freeride
                New Member
                • Feb 2009
                • 36

                #8
                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!

                Comment

                Working...