VSTO 3.0 for Office 2007 Programming
上QQ阅读APP看书,第一时间看更新

Creating our first example

In our first example, we will make you more comfortable, by explaining the Hello World example using Visual Studio 2008 and Microsoft Office InfoPath 2007.

Let's write a Hello World program as our first program, using VSTO 3.0 for Microsoft InfoPath 2007.

  1. Open Visual Studio 2008, and create a new InfoPath 2007 Form template project.
  2. Select New project. Under Office, select 2007 and then select InfoPath 2007 Form template, and name the project as per your requirements.
  3. Next, the Design Template dialog box appears. This is where you choose the template for your design requirement. In our example, we will select Blank and click on OK.
  4. The solution will be created with all of supporting files for our development of InfoPath solution. Let's write a Hello World message on a button click event for an InfoPath form. In the design task pane of the Visual Studio window, you can find the Controls button hyper linked. The following screenshot shows the InfoPath Design Task Pane inside Visual Studio.
    Creating our first example
  5. Next drag-and-drop a textbox to display the Hello World message and a button to write the Click event for, from InfoPath toolbox. The next image represents the InfoPath Toolbox Task Pane showing the InfoPath-supported controls.
    Creating our first example
  6. Right-click on the button and select Button Properties from the shortcut menu. In the Button Properties window, click on the Edit Form Code button under the General tab. This will generate the Click event for the button.
  7. Add the following code into the control's Click event:
    public void Click_Clicked(object sender, ClickedEventArgs e)
    {
    // Read the Textbox and write the "Hello World"
    XPathNavigator xNode = MainDataSource.CreateNavigator(). SelectSingleNode("/my:myFields/my:field1", NamespaceManager);
    // Set value to Textbox
    xNode.SetValue("Hello World");
    }
    

Once you have added and executed the above code, you will get the following screenshot as the output.

Creating our first example