Freigeben über


Add C# or Visual Basic to your existing form using VSTO

Have you ever created an InfoPath form to later discover that you have to write code in order to accomplish something you need?

 

If so, you have one of two options:

 

You can use VSTA to add managed code to your form.  It is included in the Office package, but it is turn off by default.  You must go the Add/Remove Programs and customize Office to install it.

 

-- OR --

 

You can use VSTO to add managed code to your form which is what this blog entry is about.

 

When you create an InfoPath Form Template using Visual Studio 2005, you start with the Design a Form dialog.  On the left you’ll see:

 

 

Choosing one of these options will allow you to create a project using an existing form template.  For example, if you wanted to use a form template from a SharePoint site, you would choose the second option.  You can also choose the last option to import an Excel workbook or a Word document as your form template.

 

If you've selected an existing form template, you will get a message:

 

 

If you've selected a form template that already had code behind it, the existing code will be removed from the new form template as well.

 

VSTO will now create a project based on the form you've selected and you're ready to add code!

 

 

- Gary

Software Development Engineer

Comments

  • Anonymous
    July 06, 2006
    The comment has been removed

  • Anonymous
    July 06, 2006
    The comment has been removed

  • Anonymous
    December 06, 2006
    How can I know for sure that form is loaded into formControl1? There is no event that fires or property being set. Is there a way to monitor something on the formControl ? -Boris Kleynbok

  • Anonymous
    December 11, 2006
    There is no property explicitly for this. You can try a few things

  1. check if the XDocument property != null. You will need to implement some sort of polling/callback mechanism for this yourself.
  2. handle the OnLoad or OnViewSwitch events from your host application (using the IConnectionPointContainer interface for C++ and delegates for .NET). .NET snippet: namespace IPHosted {    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            // sync to the startup event where you can register for individual events            formControl1.InternalStartup += new Microsoft.Office.InfoPath.FormControl.EventHandler<EventArgs>(formControl1_InternalStartup);        }        void formControl1_InternalStartup(object sender, EventArgs e)        { // sync to the View switch event (you can also sync to other form events, such as Save, Merge, etc)            ((FormControl)sender).EventManager.FormEvents.ViewSwitched += new ViewSwitchedEventHandler(OnSwitchView); ...
  3. register to receive a callback during the Init event. This event fires before document OnLoad. There should be documentation on MSDN for the details of this. Basically you need to implement IInitEventHandler interface and call its SetInitEventHandler passing in pointer to your callback function.