none
How do I show the user which button they clicked? RRS feed

  • Question

  • I am using VB 2008 and I have created a Ribbon Design add-in for Excel 2007.  On this ribbon I have buttons in a drop-down menu.  When the user clicks a button, I want a message box to popup and show the user which button they clicked.  I know I can just place code in the buttons click event to show a message box stating what button they clicked.  But, I want to programatically get the label from the button that was clicked.

    For example: I have many buttons in this drop-down menu, but lets say the user clicks on the button labeled "Water".  How do I progamatically get the label from the button and show the user that "You clicked the Water button."?

    Thanks...

    Saturday, April 21, 2012 2:46 AM

Answers

  • Hi Fdegree, 

    Welcome to MSDN Forum.

    According to your description, I recommend you to try the sample code below for your issue.

    Code snippet:

    //Add this to your ribbon.Designer.cs file.

    //For this sample code, I added 3 buttons in total.

    this.button1.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button1_Click);

    this.button2.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button2_Click);

    this.button3.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button3_Click);

    //Add this to your ribbon.cs file.

    public void button1_Click(object sender, RibbonControlEventArgs e)

    {

        MessageBox.Show(String.Format("You clicked the {0} button.", button1.Label));

    }

    public void button2_Click(object sender, RibbonControlEventArgs e)

    {

        MessageBox.Show(String.Format("You clicked the {0} button.", button2.Label));

    }

    public void button3_Click(object sender, RibbonControlEventArgs e)

    {

        MessageBox.Show(String.Format("You clicked the {0} button.", button3.Label));

    }

    With the code above, you can see the label of the button you’ve clicked in the popuped messagebox.

    Hope it helps.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bruce Song Wednesday, April 25, 2012 7:51 AM
    Monday, April 23, 2012 6:39 AM

All replies

  • Hi Fdegree, 

    Welcome to MSDN Forum.

    According to your description, I recommend you to try the sample code below for your issue.

    Code snippet:

    //Add this to your ribbon.Designer.cs file.

    //For this sample code, I added 3 buttons in total.

    this.button1.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button1_Click);

    this.button2.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button2_Click);

    this.button3.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(button3_Click);

    //Add this to your ribbon.cs file.

    public void button1_Click(object sender, RibbonControlEventArgs e)

    {

        MessageBox.Show(String.Format("You clicked the {0} button.", button1.Label));

    }

    public void button2_Click(object sender, RibbonControlEventArgs e)

    {

        MessageBox.Show(String.Format("You clicked the {0} button.", button2.Label));

    }

    public void button3_Click(object sender, RibbonControlEventArgs e)

    {

        MessageBox.Show(String.Format("You clicked the {0} button.", button3.Label));

    }

    With the code above, you can see the label of the button you’ve clicked in the popuped messagebox.

    Hope it helps.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bruce Song Wednesday, April 25, 2012 7:51 AM
    Monday, April 23, 2012 6:39 AM
  • Thank you for your response...it truly helps

    Monday, April 23, 2012 2:33 PM