none
VSTO Outlook: Create buttons in Run-Time - where can I find the constructor? RRS feed

  • Question

  • All

    I am trying to add a button in a ribbon during runtime and found article below

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6408b587-db86-478b-b08d-5bb0da27c852/dynamically-add-ribbongroup-and-controls-at-runtime?forum=vsto

    At end of the article it is said the code must be put inside the constructor. and not in the load-event of the ribbon  But where can I find the constructor?  I am new into Visual Studio and VB.NET (I was able to convert the original code).  I do find a sub "Private Sub ThisAddIn_Startup() Handles Me.Startup" but if I put the code there, I get errors (as "me" and "grpuser" cannot be found)

    By the way: grpUser is the name of my RibbonGroup.

    Dim rbTemp = Me.Factory.CreateRibbonButton()
    rbTemp.Name = "rbtnTemp"
    rbTemp.ControlSize = Office.RibbonControlSize.RibbonControlSizeLarge
    rbTemp.Description = "I was created dynamically..."
    rbTemp.Label = "I was created dynamically..."
    rbTemp.ScreenTip = "I was created dynamically..."
    Me.grpUser.Items.Add(rbTemp)

    Regards
    Ino

    • Edited by ino_mart Wednesday, March 25, 2020 3:32 PM
    Wednesday, March 25, 2020 8:57 AM

Answers

  • In meantime I found the constructor is the NEW-event, so somehow I must merge code above in one of the subs below. I copied/pasted code above in the first "New"-sub. Although it does not give any error message, the new button is not in the ribbon when I launch the program. If I put the code in the second NEW-sub I do get errors, so most probably the code must be in some other form in the first NEW-sub.

    Partial Class rbnMirion
        Inherits Microsoft.Office.Tools.Ribbon.RibbonBase
        <System.Diagnostics.DebuggerNonUserCode()>
        Public Sub New(ByVal container As System.ComponentModel.IContainer)
            MyClass.New()
            'Required for Windows.Forms Class Composition Designer support
            If (container IsNot Nothing) Then
                container.Add(Me)
            End If
        End Sub
        <System.Diagnostics.DebuggerNonUserCode()>
        Public Sub New()
            MyBase.New(Globals.Factory.GetRibbonFactory())
            'This call is required by the Component Designer.
            InitializeComponent()
        End Sub

    • Edited by ino_mart Wednesday, March 25, 2020 3:31 PM
    • Marked as answer by ino_mart Friday, March 27, 2020 11:29 AM
    Wednesday, March 25, 2020 3:29 PM

All replies

  • In meantime I found the constructor is the NEW-event, so somehow I must merge code above in one of the subs below. I copied/pasted code above in the first "New"-sub. Although it does not give any error message, the new button is not in the ribbon when I launch the program. If I put the code in the second NEW-sub I do get errors, so most probably the code must be in some other form in the first NEW-sub.

    Partial Class rbnMirion
        Inherits Microsoft.Office.Tools.Ribbon.RibbonBase
        <System.Diagnostics.DebuggerNonUserCode()>
        Public Sub New(ByVal container As System.ComponentModel.IContainer)
            MyClass.New()
            'Required for Windows.Forms Class Composition Designer support
            If (container IsNot Nothing) Then
                container.Add(Me)
            End If
        End Sub
        <System.Diagnostics.DebuggerNonUserCode()>
        Public Sub New()
            MyBase.New(Globals.Factory.GetRibbonFactory())
            'This call is required by the Component Designer.
            InitializeComponent()
        End Sub

    • Edited by ino_mart Wednesday, March 25, 2020 3:31 PM
    • Marked as answer by ino_mart Friday, March 27, 2020 11:29 AM
    Wednesday, March 25, 2020 3:29 PM
  • All

    I found the solution.  There is a "private sub InitializeComponent" where you have to put the code in.

    The only extra thing to add is the Addhandler
    AddHandler rbTemp.Click, AddressOf btnCode_Click

    Friday, March 27, 2020 11:29 AM