events (gui)

  • Thread starter csharpula csharp
  • Start date
C

csharpula csharp

Hello,

I would like to know hoe can I retrieve from the following info I get in
event args ,on which item of context menu strip i am clicking now?

I get Mouse Events Args in my handler after clicking on some item of the
context menu strip and need to translate it to some
menuStrip.Items[index] .How do I get this index?

Thanks a lot!
 
J

Jeff Gaines

I get Mouse Events Args in my handler after clicking on some item of the
context menu strip and need to translate it to some
menuStrip.Items[index] .How do I get this index?

The object 'sender' is the Menu Item that was clicked so if you cast that
to a ToolstripMenuItem you have access to all its properties.
 
J

Jeff Johnson

I would like to know hoe can I retrieve from the following info I get in
event args ,on which item of context menu strip i am clicking now?

I get Mouse Events Args in my handler after clicking on some item of the
context menu strip and need to translate it to some
menuStrip.Items[index] .How do I get this index?

I have found that depending on indexes is a very fragile way of programming.
I think you'd be better off attaching some ID value (an enum, perhaps?) to
each menu item's Tag. That way if you have a situation where the same event
handler processes multiple menu items you can identify the source item
easily. It saves you having to update code if you insert a new item (and
therefore change the indexes of all the following items).
 
D

Dawid Rutyna

Hello,

I would like to know hoe can I retrieve from the following info I get in
event args ,on which item of context menu strip i am clicking now?

I get Mouse Events Args in my handler after clicking on some item of the
context menu strip and need to translate it to some
menuStrip.Items[index] .How do I get this index?

You can try something like that:

private void contextMenuStrip1_ItemClicked(object sender,
ToolStripItemClickedEventArgs e)
{
int index = ((ContextMenuStrip)sender).Items.IndexOf(e.ClickedItem);
if (index > 0) this.Text = index.ToString(CultureInfo.CurrentCulture);
}

Dawid Rutyna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top