locked
VSTO - Excel Add-In and Custom Forms RRS feed

  • Question

  • Hi all,

    I have very little knowledge of VSTO and Forms, and unfortunately I have a project that requires both.  I'm a novice programmer at best, and haven't programmed in over a year, so I forget alot of simple things that I just can't figure out.

    I need to develop an Add-In for Excel 03 & 07 that creates a new Menu Item with a button. The button will launch a custom Form that take in some numbers (from TextBoxes, not from the sheet), does some calculations, and spits the results into the Excel spreadsheet. Can I use C#? Or does this have to be done in VB?

    So far I have the Menu Item added with a button (Add-Ins->"My Menu"-> "Do Stuff"). When I click the button I can pop-up a dialog box, but thats about it. (in C# btw)

    I need some help on the Form. How do I even add the Form to my project (I can design it once its there)? How do I "launch" the Form when I click my button? Hopefully I can handle the data management (taking in data from TextBoxes and doing some calculation), but I can't even get to that point yet.

    I know this is all really simple stuff, and once I break a few barriers I think I can get by, but I'm missing alot of basics. No tutorial that I've found starts from the ground up - they all assume you have some prior knowledge of either VSTO or Forms.

    Thanks for your time,
    Shaun.
    Saturday, November 21, 2009 1:50 AM

Answers

  • Hi Shaun!

    Incorporating forms (Windows Forms) into your addin is as simple as adding a Windows Forms class into your project.
    In the Solution Explorer, right-click on your project, and choose Add > Windows Form... .

    In your Windows Form class, add your textboxes and expose the properties that will correspond to the user's input in those textboxes:


    public partial class TextWindow : System.Windows.Forms.Form
    {
        public string TextContent 
        {
            get
            {
                return textBox1.Text;
            }
        }
    }


    After designing the form, displaying it is just a matter of calling the ShowDialog() method inside your menu button's Click event:


    void menuItemButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
    {
        TextWindow inputForm = new TextWindow();

        inputForm.ShowDialog();

        string userInput = inputForm.TextContent;
    }

    Saturday, November 21, 2009 2:49 AM

All replies

  • Hi Shaun!

    Incorporating forms (Windows Forms) into your addin is as simple as adding a Windows Forms class into your project.
    In the Solution Explorer, right-click on your project, and choose Add > Windows Form... .

    In your Windows Form class, add your textboxes and expose the properties that will correspond to the user's input in those textboxes:


    public partial class TextWindow : System.Windows.Forms.Form
    {
        public string TextContent 
        {
            get
            {
                return textBox1.Text;
            }
        }
    }


    After designing the form, displaying it is just a matter of calling the ShowDialog() method inside your menu button's Click event:


    void menuItemButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
    {
        TextWindow inputForm = new TextWindow();

        inputForm.ShowDialog();

        string userInput = inputForm.TextContent;
    }

    Saturday, November 21, 2009 2:49 AM
  • Hi Jose,
    Thanks for the quick reply.

    I must be doing something wrong because I'm getting some errors on Build.

    The funtion: "public partial class TextWindow : System.Windows.Forms.Form{...}" goes in the Form file? "InputForm01.cs" in my case?

    and

    The funtion: "void menuItemButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault){...}" goes in the "ThisAddIn.cs" file?

    I get errors about "TextWindow" in the "Button_Click" function - "not found" - because its defined in another file I think? And I also get errors about textBox1 "Cannot access a non-static member of outer type 'ContractEvaluation.InputForm01' via nested type 'ContractEvaluation.InputForm01.TextWindow' ".

    I'm sure I'm just doing something wrong..

    Thanks again,
    Shaun
    Saturday, November 21, 2009 5:44 AM
  • Hi again!

    If I'm not mistaken, you have a form class called InputForm01 and inside the code for this class,
    you also added the TextWindow class. Is that what you did?

    If yes, you no longer need the TextWindow class. The code I gave you assumes that the name of your form is TextWindow.
    Simply add the TextContent property to your InputForm01 class,
    and make sure that the InputForm01 class has a text box named textBox1 (which you can rename).

     
    Saturday, November 21, 2009 6:21 AM
  • Great, thanks for clearing that up - I knew I was doing something silly.

    Everything is moving along smoothly.. for now :)

    Shaun.
    Saturday, November 21, 2009 10:24 PM