none
Change text and picture in a ribbon with vb code? RRS feed

  • Question

  • Hi,

    I have programmed a MS-Project Add-In with VST2010.

    To controlled my functions, I have designed a ribbon with VST2010.

    How can I change the text and the picture in the elements of ribbons with visual basic code during the Add-In is running?

    Thank you!

       Michael

    Thursday, April 28, 2011 2:09 PM

Answers

  • Hi Michael

    When you use the Ribbon Designer, you address the controls in your code pretty much the same as if they were WinForms controls. So, for example

      Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        Dim btnImage As System.Drawing.Image = System.Drawing.Bitmap.FromFile("C:\Users\Cindy Meister\Pictures\Avatar.jpg")
        Me.Button2.Image = btnImage
    
      End Sub
    
    

    I assign the control Button2.Image property a System.Drawing.Image object, in this case a graphic from a file on my machine.

    The following snippet shows how to change the label of a control on the event of another control:

      Private Sub CheckBox2_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles CheckBox2.Click
        If Me.CheckBox2.Checked Then
          Me.Button2.Label = "checked"
        Else
          Me.Button2.Label = "unchecked"
        End If
      End Sub
    

     


    Cindy Meister, VSTO/Word MVP
    Saturday, April 30, 2011 7:02 AM
    Moderator
  • Hi Michael

    You can only access items through the Ribbon class, but you can use this elsewhere in your solution. Sample line of code based on my previous example:

      Globals.Ribbons.Ribbon1.Button2.Label = _
        Me.Application.ActiveDocument.Name

    And reference in the documentation: http://msdn.microsoft.com/en-us/library/bb772088.aspx


    Cindy Meister, VSTO/Word MVP
    Sunday, May 1, 2011 8:16 AM
    Moderator

All replies

  • Hi Michael

    First, we need to know whether you added a "Ribbon Designer" to your add-in project, or Ribbon XML?


    Cindy Meister, VSTO/Word MVP
    Thursday, April 28, 2011 3:29 PM
    Moderator
  • Hi!

    I have used the "Ribbon Designer"

    Now I have followed file:

    Ribbon2010.vb
    -> Ribbon2010.Designer.vb
    -> Ribbon2010.resx

    Thank you!

      Michael

     

    Thursday, April 28, 2011 4:06 PM
  • Hi Michael

    When you use the Ribbon Designer, you address the controls in your code pretty much the same as if they were WinForms controls. So, for example

      Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        Dim btnImage As System.Drawing.Image = System.Drawing.Bitmap.FromFile("C:\Users\Cindy Meister\Pictures\Avatar.jpg")
        Me.Button2.Image = btnImage
    
      End Sub
    
    

    I assign the control Button2.Image property a System.Drawing.Image object, in this case a graphic from a file on my machine.

    The following snippet shows how to change the label of a control on the event of another control:

      Private Sub CheckBox2_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles CheckBox2.Click
        If Me.CheckBox2.Checked Then
          Me.Button2.Label = "checked"
        Else
          Me.Button2.Label = "unchecked"
        End If
      End Sub
    

     


    Cindy Meister, VSTO/Word MVP
    Saturday, April 30, 2011 7:02 AM
    Moderator
  • Hi Cindy,

     

    Thank you for your answer, but I need the access to the ribbon from outside the class, in other word not with "Me." within the class.

    My ControlId from the tab is "PMAssistantTab" and the command for setting the label must similary:

    Microsoft.Office.Tools.Ribbon.....Item("PMAssistantTab").label = "PM Assistant"

    Can you tell me the correct statement?

    Thank you!   Michael

     

    Saturday, April 30, 2011 3:54 PM
  • Hi Michael

    You can only access items through the Ribbon class, but you can use this elsewhere in your solution. Sample line of code based on my previous example:

      Globals.Ribbons.Ribbon1.Button2.Label = _
        Me.Application.ActiveDocument.Name

    And reference in the documentation: http://msdn.microsoft.com/en-us/library/bb772088.aspx


    Cindy Meister, VSTO/Word MVP
    Sunday, May 1, 2011 8:16 AM
    Moderator