Search code examples
c#toolstripmenu

How to get selected index of toolstripbutton in c#


I have a toolstrip control on a form and i programatically add buttons to the toolstrip control using the below code

            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);

Now i m trying to get the index of the toolstrip button when i click on it note i can get the text of the clicked toolstripbutton using

e.ClickedItem.Text;

Solution

  • There isn't an index property on the toolstripitem click but you could do something like this

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

    Where the Tag property is something you set as the index.