Addin doesnt open window or messagebox

Addin doesnt open window or messagebox

hlingen
Community Visitor Community Visitor
194 Views
2 Replies
Message 1 of 3

Addin doesnt open window or messagebox

hlingen
Community Visitor
Community Visitor

Like in the title. My inventor addin doesnt open a window or messagebox and i cant figure out why.

My code is as follows:

public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
{
    inventorApplication = addInSiteObject.Application;

    // Create a new RibbonTab
    RibbonTab ribbonTab = inventorApplication.UserInterfaceManager.Ribbons["Part"].RibbonTabs.Add("HorizonTab", "Horizon Tab", "a86e9d80-d720-4020-97b2-244323f99115");

    // Create a new RibbonPanel
    RibbonPanel ribbonPanel = ribbonTab.RibbonPanels.Add("HorizonPanel", "Horizon Panel", "a86e9d80-d720-4020-97b2-244323f99115");

    // Create a ButtonDefinition for opening a window
    ButtonDefinition openWindowButton = inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
        "OpenWindowButton",
        "Open Window",
        CommandTypesEnum.kEditMaskCmdType,
        "{yourClientId}",
        "Sample text for opening a window",
        "icon_open_window.ico"
    );

    // Create a ButtonDefinition for displaying a message box
    ButtonDefinition messageBoxButton = inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
        "MessageBoxButton",
        "Show Message",
        CommandTypesEnum.kEditMaskCmdType,
        "{yourClientId}",
        "Sample text for displaying a message box",
        "icon_message_box.ico"
    );

    // Register event handlers for button clicks
    openWindowButton.OnExecute += OpenWindowHandler;
    messageBoxButton.OnExecute += MessageBoxHandler;

    // Add the buttons to the ribbon panel
    ribbonPanel.CommandControls.AddButton(openWindowButton, false);
    ribbonPanel.CommandControls.AddButton(messageBoxButton, false);
}
private void OpenWindowHandler(NameValueMap context)
{
    // Debugging check
    System.Diagnostics.Debug.WriteLine("OpenWindowHandler called");

    // Implement the code to open a window here (using Windows Forms)
    Form window = new Form();
    window.Text = "Sample Window";
    window.Size = new System.Drawing.Size(400, 200);
    window.Controls.Add(new Label() { Text = "This is a sample window." });
    // Start a message loop for the window
    System.Windows.Forms.Application.Run(window);
    // window.ShowDialog();
}

private void MessageBoxHandler(NameValueMap context)
{
    // Debugging check
    System.Diagnostics.Debug.WriteLine("MessageBoxHandler called");

    // Display a message box with the sample text
    MessageBox.Show("Sample text for displaying a message box", "Message Box");
}

 

Can someone help me with this. Note, it is my first programming in C#. 

0 Likes
195 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor

in the first lines of your activate method, you add a RibbonTap and a RibbonPanel. That can be an issue if they already exist. Therefore you should check if it already exists before you try to add them.

 

Also, I would suggest surrounding everything in the activate method in a try/catch block. so you know if something errored out. More info on that in my blog post: "Help: My addin won't load"

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 3

hlingen
Community Visitor
Community Visitor

Thanks for your answer, I will add a try catch block around the method. I made some progress though which creates more questions.

When i ran the code block as is in Debug mode, set breakpoints on the `button.onExecute` lines, it suddenly worked. 
Building the addin, and it doesnt anymore. Have you seen this before?

0 Likes