如何在 C# 中获取 toolstripbutton 的选定索引

How to get selected index of toolstripbutton in c#

我在表单上有一个工具条控件,我使用以下代码以编程方式将按钮添加到工具条控件

            toolStrip1.Visible = true;
            ToolStripItem t = new ToolStripButton();
            t.Text = client.EndPoint.ToString();
            t.TextImageRelation = TextImageRelation.ImageAboveText;
            t.BackgroundImage = Image.FromFile("" + Application.StartupPath + "ps1_new.PNG");
            t.AutoSize = false;
            t.Height = 67;
            t.Width = 70;
            t.BackgroundImageLayout = ImageLayout.Stretch;
            t.TextAlign = ContentAlignment.BottomCenter;
            toolStrip1.Items.Add(t);

现在我试图在单击时获取工具条按钮的索引 请注意,我可以使用

获取单击的工具条按钮的文本
e.ClickedItem.Text;

toolstripitem click 上没有索引 属性,但您可以这样做

    private void ToolStrip1_ItemClicked(object sender, EventArgs e)
    {
        MessageBox.Show(e.ClickedItem.Tag)
    }

标签 属性 是您设置为索引的内容。