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.