获取 ContextMenuStrip 中 ToolstripItem 的文本

发布于 2024-11-17 12:31:59 字数 516 浏览 5 评论 0原文

我有一个 DGV 的上下文菜单条。它有一个名为“ChangeTo”的工具条项目,它分支为一组根据所使用的配置文件动态创建的项目。当我运行具有 Rt-Click 的程序时 ->更改为 -> (项目列表)

当我单击 contextmenustrip 中下拉列表中的任何项目时,我希望 DGV 的选定行更改为列表中的文本...

为此,我需要获取与关联的“文本”工具条项。我该怎么做? 我不能只使用 toolstripitemname.text 因为直到运行时我才知道项目名称...我尝试使用

ChangeTotoolstripitem.DropDown.Items... 

但我需要索引...

这是单击项目时我使用的功能

private void changeTypeToToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蹲墙角沉默 2024-11-24 12:31:59

当您拥有 ToolStripItemClickedEventArgs 时,这不会起作用吗?:

string toolstripItemName = e.ClickedItem.Text;

Will this not work as you have the ToolStripItemClickedEventArgs?:

string toolstripItemName = e.ClickedItem.Text;
酷到爆炸 2024-11-24 12:31:59

使用

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

use

private void changeTypeToToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
   string clickedtext=e.ClickedItem.Text;
}
欲拥i 2024-11-24 12:31:59

我不得不读几次这篇文章,但我认为这就是你所追求的:

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

这就是你所追求的吗?您可以轻松获取控件的名称 (mi.Name) 或其他任何名称。

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.

浊酒尽余欢 2024-11-24 12:31:59
ToolStripItem item = e.ClickedItem;
Console.WriteLine("++ clicked item ->{0}[{1}]  of {2}", item.Name, item.Text, item.Owner.Name);
ToolStripItem item = e.ClickedItem;
Console.WriteLine("++ clicked item ->{0}[{1}]  of {2}", item.Name, item.Text, item.Owner.Name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文