none
How can I know the name/title/label/ID (whatever) of the button just clicked ? RRS feed

  • Question

  • I made a button on a document using Word VSTO Ribbon Addin button.

    When I click it (the one on the document), I'd like to show the name of button just clicked.

    It does not matter it shows name/title/text/label/ID or what so ever it can distinguish/tell the button just clicked.

    I will have a plenty of button, I need to distingush each and every one.

    Regards

    private void  VuTTon_KLiK(object sender, EventArgs e)
    	{
    			MessageBox.Show( "You've just clicked : " + "???" + " button." );
    	}
    private void AsK_Click( object sender, RibbonControlEventArgs e )
    	{
    			Microsoft.Office.Interop.Word.Document	 N_DoCu = 
    			Globals.ThisAddIn.Application.ActiveDocument;
    
    			Microsoft.Office.Tools.Word.Document V_DoCu = 
    			Globals.Factory.GetVstoObject( N_DoCu );
    
    			Microsoft.Office.Tools.Word.Controls.Button	 VuTToN  = 
    			V_DoCu.Controls.AddButton( Globals.ThisAddIn.Application.Selection.Range, 100, 20, "What's_This" );
    
    			VuTToN.Text = "Serial_Number" ;
    
    			Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.Zoom.Percentage = 100; 
    
    			VuTToN.Click += new EventHandler(VuTTon_KLiK);
    	}


    Sunday, March 15, 2015 8:08 AM

Answers

  • Hello,

    The sender argument passed to the event handler represents the button which fired the event. You can cast the sender object to the Button class and get all the required property values.

    Sunday, March 15, 2015 12:43 PM

All replies

  • What do I have to do to fill the "???" part properly ?

    The best one is to display "Serial_Number" on the message box.

    How can I do it ??


    Sunday, March 15, 2015 8:10 AM
  • private void  VuTTon_KLiK(object sender, EventArgs e)
        {
                MessageBox.Show( Regex.Replace( sender.ToString(), @".*?Text: (.*)", "$1")) ;
        }

    I make some workaround...

    Could you tell me the better way ?


    Sunday, March 15, 2015 8:53 AM
  • Hello,

    The sender argument passed to the event handler represents the button which fired the event. You can cast the sender object to the Button class and get all the required property values.

    Sunday, March 15, 2015 12:43 PM
  • Thank for the nice reply.

    I figured it out like this. Is it good one ?

    MessageBox.Show(  ( ( (dynamic) sender).Text ));

    Regards.




    Sunday, March 15, 2015 2:54 PM
  •             RibbonButton rb = (RibbonButton)sender;

                MessageBox.Show(rb.Label);  ''for ribbon's label

                MessageBox.Show(rb.Name); "name

                MessageBox.Show(rb.Id);  "ID

    if you want "Serial_Name", use this:       MessageBox.Show(rb.Label);

    Monday, March 16, 2015 10:20 AM
  • All righty.

    That's good one too.

    Thanks hdzmhz.

    I used like this.

                Button bt = (Button) sender;
                MessageBox.Show( bt.Name);
                MessageBox.Show( bt.Text);



    Monday, March 16, 2015 1:22 PM