How can I show a windows form when I press a ribbon button?

910 views
Skip to first unread message

al...@unleashreality.com

unread,
Feb 18, 2014, 5:33:46 AM2/18/14
to exce...@googlegroups.com
Hi everyone! :)

Quick question: In this thread - there's a question on how to create a custom dialog box for user input and Govert suggests "create a Windows Forms or WPF dialog and display from a ribbon, menu 
or command bar button."

I have a c# addin with a working CTP (based on the CustomTaskPane sample) so I have my usual  public class MyRibbon : ExcelRibbon etc.

My question is how can I add my newly created windows forms "myForm" to my ribbon so that it shows when I press my ribbon button?

I've added my ribbon button to MyRibbon class public void ShowMyForm(IRibbonControl control) and linked it to my Ribbon dna - just not sure what to put in ShowMyForm so that my form comes up when I press the ribbon button? (the taskpane uses the CTPManager which is a little bit of a black box to me right now to be honest)

Thank you for your help and have a great day!

Regards,
Alex

Govert van Drimmelen

unread,
Feb 19, 2014, 5:19:44 AM2/19/14
to exce...@googlegroups.com
Hi Alex,

Maybe you can just make a new form instance and call .ShowDialog().

    var frm = new MyInfoForm();
    frm.ShowDialog();

-Govert

al...@unleashreality.com

unread,
Feb 19, 2014, 6:05:44 AM2/19/14
to exce...@googlegroups.com
Hi Govert,

Thanks so much for replying and please forgive my newbie questions!

I tried replacing the OnDeleteCTP method that the ribbon button fires with the following:

        public void OnDeleteCTP(IRibbonControl control)
        {
            //CTPManager.DeleteCTP();
            MessageBox.Show("try");
            var frm = new myForm();
            frm.ShowDialog();
            MessageBox.Show("success");
        }

And it shows the first messagebox "try" but no form and does not show the second messagebox "success".

Sorry again for such stupid questions and thank you so much for your help and great work

Jiri Pik

unread,
Feb 19, 2014, 6:10:16 AM2/19/14
to exce...@googlegroups.com

Search this forum for more snippets. There have been a few in the last year relevant for you. You are after new thread with STA.

 

 

You should not use ShowDialog if you do not want the form to be not modal.

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/groups/opt_out.

Govert van Drimmelen

unread,
Feb 19, 2014, 6:15:58 AM2/19/14
to exce...@googlegroups.com
Hi Alex,

Maybe there's an exception being thrown - you can wrap in a try-catch block and show the exception.

-Govert

al...@unleashreality.com

unread,
Feb 20, 2014, 5:13:56 AM2/20/14
to exce...@googlegroups.com
Ahhh that was it thank you so much Govert!

This is working:
MessageBox.Show("try");
            try
            {
                var frm = new myForm();
                frm.ShowDialog();
            }
            catch (Exception y)
            {
                MessageBox.Show(y.StackTrace + " " + y.Message);
            }
            MessageBox.Show("success");
Reply all
Reply to author
Forward
0 new messages