in

Data Techniques Forums

How to add Thumbnail viewer control right click menu like delete,select.....

Last post 02-02-2010 3:36 PM by brianb. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 01-22-2010 9:51 AM

    How to add Thumbnail viewer control right click menu like delete,select.....

    Hi All,

    Thank you for advance.

    We are using VS2008 with C# window forms.

    How to add right click menu like delete,select....  on Thumbnail viewer control  and also how to implement that actions?

    give me the sample code in c#.

     


     

  • 01-25-2010 12:33 PM In reply to

    Re: How to add Thumbnail viewer control right click menu like delete,select.....

    As I noted in an earlier post: you can easily add a context menu to the Thumbnail control and have it only display when the mouse is over a Thumbnail (as opposed to also being visible when it's over the space between Thumbnails). 

    The steps are as follows:

    • On your form's design surface, drop a ContextMenuStrip onto the form
    • Add Menu Items for each operation you want to perform (delete, select, etc.)
    • Hook up to the Thumbnail controls MouseUp event
    • Add the following code to ensure that the context menu only displays when you are actually right clicking on a thumbnail (as opposed to right clicking inside the margins of the Thumbnail control):

    ThumbNailViewer1_MouseUp(object sender, MouseEventArgs e)

    {

       if (e.Button == Windows.Forms.MouseButtons.Right & ThumbNailViewer1.GetThumbnailFromPosition(e.X, e.Y) != null) {
            ContextMenuStrip1.Visible = true;
        }
        else {
            ContextMenuStrip1.Visible = false;
        }
    }

    • Write your own logic for each menu item operation

     

    That should be enough the get you started. Remember, you can use the click event args for the Thumbnail Click event to determine the index of the currently selected thumbnail.

     

    So, with this post and the previous responses I gave you, you should have more than enough information to create a right-click context menu and get it functional.

    Thanks,

    Brian
    Technical Support
    Data Techniques
    Support Options
  • 01-29-2010 9:19 AM In reply to

    Re: How to add Thumbnail viewer control right click menu like delete,select.....

     

    Hi,

    I need to get the index of the Thumbnail viewer when i right click. but i can get the index of selected Thumbnail viewer item.

    in below screenshot i selected 3rd item in Thumbnail viewer. and i can get the index of this item. when i right click on first item how to get the index of first item(which is not selected).

    I am not able to change the EventArgs to Thumbnail Click event Args.

    private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
    {
    //code
    }

    How to pass  deleteToolStripMenuItem1_Click function argument of  Thumbnail Click event instead of EventArgs

    please give me the solution with c# sample code.

     

  • 02-02-2010 3:36 PM In reply to

    Re: How to add Thumbnail viewer control right click menu like delete,select.....

    Here's how I coded a test app to retrieve the index of the currently right-clicked thumbnail and select it:

    private ThumbNail rightClickThumbnail = null;

    private void thumbNailViewer1_MouseUp(object sender, MouseEventArgs e)
    {
        rightClickThumbnail = thumbNailViewer1.GetThumbnailFromPosition(e.X, e.Y);

        if (e.Button == MouseButtons.Right &  rightClickThumbnail != null)
        {
            contextMenuStrip1.Visible = true;

            // code to get the context menu placement correct
            contextMenuStrip1.Top = e.Y + this.Top + thumbNailViewer1.ThumbSize.Height;
            contextMenuStrip1.Left = e.X + this.Left;
        }
        else
        {
            contextMenuStrip1.Visible = false;
            rightClickThumbnail = null;
        }
    }

    private void selectToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (rightClickThumbnail != null)
        {
            int index = thumbNailViewer1.Thumbs.IndexOf(rightClickThumbnail);

            SelectThumbNail(thumbNailViewer1.Thumbs, index);
            viewer1.Images.CurrentImageIndex = index;
            thumbNailViewer1.Refresh();
            rightClickThumbnail = null;
        }
    }

    private void SelectThumbNail(
        List<ThumbNail> Thumbs,
        int currentSelectedIndex)
    {
        for (int i = 0; i < Thumbs.Count; i++)
        {
            if (i == currentSelectedIndex)
            {
                Thumbs[i].Selected = true;                   
                _currentSelectedIndex = currentSelectedIndex;
            }
            else
            {
                Thumbs[i].Selected = false;
            }
        }
    }

     

    So, in essence, I store the thumbnail that is retrieved in the right-click operation (in a private variable called rightClickThumbnail ) and then get use the Thumbs collection to retreive it's index (in the selectToolStripMenuItem_Click event).  I wrote a helper method to help me select a Thumbnail at a given index in the Thumbs property (called SelectThumbNail). 

    Don't forget to set the stored Thumbnail back to null after you are done with it so you don't cause some sort of false-positive problem later on in your code.

    I've only shown you the code to handle selection, but you get the gist of it, I'm sure, so you can write the rest of your business logic using the same reasoning here.

    Let me know if this works for you.

    Brian
    Technical Support
    Data Techniques
    Support Options
Page 1 of 1 (4 items)
Copyright 2009 Data Techniques, Inc. All Rights Reserved.