techmanc
techmanc

Reputation: 293

Getting the Text of ToolstripItem in a ContextMenuStrip

I have a contextmenustrip for a DGV. It has a toolstripitem called "ChangeTo" and this branches out into a set of items that are created dynamically based on the config file used. When I run the program that Has Rt-Click -> Change To -> (List of Items)

When I click any item from the drop down list in the contextmenustrip , I want the selected row of the DGV to change to the text in the list...

For this i need to get the 'Text' associated with the toolstripitem. How can i do this? I cant just use toolstripitemname.text coz i wouldnt know the item name until runtime... I tried using

ChangeTotoolstripitem.DropDown.Items... 

but i need the index...

This is the function i use when the item is clicked

private void changeTypeToToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)

Upvotes: 2

Views: 3088

Answers (4)

user2857584
user2857584

Reputation: 1

ToolStripItem item = e.ClickedItem;
Console.WriteLine("++ clicked item ->{0}[{1}]  of {2}", item.Name, item.Text, item.Owner.Name);

Upvotes: 0

user153923
user153923

Reputation:

I had to read this a few times, but I think this is what you are after:

private void changeTypeToToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) {
  ToolStripMenuItem mi = sender as ToolStripMenuItem;
  if (mi != null) {
    // This is your text:
    Console.WriteLine(mi.Text);
  }
}

Is that what you are after? You could just as easily get the control's name (mi.Name) or whatever else.

Upvotes: 1

hdougie
hdougie

Reputation: 344

Will this not work as you have the ToolStripItemClickedEventArgs?:

string toolstripItemName = e.ClickedItem.Text;

Upvotes: 1

Bolu
Bolu

Reputation: 8786

use

private void changeTypeToToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
   string clickedtext=e.ClickedItem.Text;
}

Upvotes: 1

Related Questions