Creating a Basic Managed Code Form Template

Taken from Microsoft MSDN…
When using Microsoft Office InfoPath 2003 to write business logic in a managed code language (Visual Basic or C#), you use an InfoPath project created with Microsoft Visual Studio .NET 2003 and the Microsoft Office InfoPath 2003 Toolkit for Visual Studio .NET or Visual Studio 2005 Tools for the Microsoft Office System with the Microsoft Office InfoPath 2003 Toolkit for Visual Studio 2005. Both of these managed code development environments depend on and support only the COM Interop managed code object model provided by the Microsoft.Office.Interop.InfoPath.SemiTrust assembly
For Microsoft Office InfoPath 2007, you can write business logic in Visual Basic or C# by opening a form template in design mode, and then using one of the user interface commands to add an event handler, which will open the Microsoft Visual Studio Tools for Applications (VSTA) development environment for writing your code. By default, form template projects created using VSTA work against the new managed code object model provided by the Microsoft.Office.InfoPath assembly.
Alternatively, if you have Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, or Visual Studio 2008 with Visual Studio Tools for Office installed, you can start from Visual Studio, create an InfoPath form template project, and then use the InfoPath design mode integration to design your form and add business logic all within the Visual Studio environment.
The first walkthrough shows you how to create a simple Hello World application using C# or Visual Basic in the VSTA and Visual Studio development environments. The walkthrough concludes with a code sample that shows you how to use the System.Environment.UserName property to retrieve the current user’s name and populate a Text Box control with that value.

 Prerequisites

In order to complete this walkthrough using the VSTA development environment, you will need:
Microsoft Office InfoPath 2007 with Microsoft Visual Studio Tools for Applications (VSTA) installed.
In order to complete this walkthrough using the Visual Studio development environment with InfoPath design mode integration, you will need:
Microsoft Office InfoPath 2007
Microsoft Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System or Visual Studio 2008 with Visual Studio Tools for Office
Note:
For information on how to download and install Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, see theInfoPath Developer Portal [ http://msdn.microsoft.com/office/understanding/infopath/default.aspx ] .

 Hello World in VSTA

In the following walkthrough, you will learn how to display a simple alert dialog box by writing an event handler for theClicked [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.buttonevent.clicked(VS.80).aspx ] event of theButtonEvent [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.buttonevent(VS.80).aspx ] class, which is associated with the Button control. These procedures assume you are using the Microsoft Visual Studio Tools for Applications (VSTA) development environment.Create a new project and specify the programming language (Visual Studio Tools for Applications)

Start InfoPath.
In the Fill Out a Form dialog box, click Design a Form Template under Design a Form.
Note:
If InfoPath is already running, to display the Design a Form dialog box, click Design a Form on the File menu.
In the Design a Form Template dialog box, click Form Template, click Blank, clear the Enable browser-compatible features only check box, and then click OK.
On the Tools menu, click Form Options.
In the Category list, click Programming, select either Visual Basic or C# from the Form template code language drop-down list, and then click OK.
You are now ready to add a Button control and create its event handler.Add a Button control and event handler

In Design Tasks task pane, click Controls.
Drag a Button control onto the form.
Double-click the Button control, type Hello for the Label property, and then click Edit Form Code. When prompted, save the form and name it HelloWorld.
This will open the Visual Studio Tools for Applications environment with the cursor in the event handler for the Clicked event of Button control.
You are now ready to add form code to the event handler for the button.Add “”Hello World”” code to the event handler and preview the form

In the event handler skeleton, type:

C#
Copy Code
MessageBox.Show(“”Hello World!””);

Visual Basic
Copy Code
MessageBox.Show(“”Hello World!””)

The code for your form template should look similar to the following:

C#
Copy Code
using Microsoft.Office.InfoPath;using System;using System.Windows.Forms;using System.Xml;using System.Xml.XPath;namespace HelloWorld {public partialclass FormCode {publicvoid InternalStartup() { ((ButtonEvent)EventManager.ControlEvents[“”CTRL1_5″”]).Clicked +=new ClickedEventHandler(CTRL1_5_Clicked); }publicvoid CTRL1_5_Clicked(object sender, ClickedEventArgs e) { MessageBox.Show(“”Hello World!””); } } }

Visual Basic
Copy Code
Imports Microsoft.Office.InfoPathImports SystemImports System.Windows.FormsImports System.XmlImports System.Xml.XPathNamespace HelloWorldPublicClass FormCodePrivateSub InternalStartup(ByVal senderAsObject,ByVal eAs EventArgs)HandlesMe.StartupAddHandlerDirectCast(EventManager.ControlEvents(“”CTRL1_5″”), ButtonEvent).Clicked,AddressOf CTRL1_5_ClickedEndSubPublicSub CTRL1_5_Clicked(ByVal senderAsObject,ByVal eAs ClickedEventArgs) MessageBox.Show(“”Hello World!””)EndSubEndClassEndNamespace

Switch to the InfoPath design mode window.
Click the Preview button on the Standard toolbar.
Click the Hello button.
A message box will be displayed with the text “”Hello World!””
The next procedure shows how to add debugging breakpoints to your form code.Debug form code

Switch back to the VSTA window.
Click the grey bar to the left of the line:

C#
Copy Code
MessageBox.Show(“”Hello World!””);

Visual Basic
Copy Code
MessageBox.Show(“”Hello World!””)

A red circle is displayed and the line of code is highlighted to indicate that the runtime will pause at this breakpoint in your form code.
On the Debug menu, click Start Debugging (or press F5).
In the InfoPath Preview window, click the Hello button.
The VSTA code editor is given focus, and the breakpoint line is highlighted.
On the Debug menu, click Step Over (or press F10) to continue stepping through the code.
The event handler code is executed, and the “”Hello World!”” message is displayed.
Click OK to return to the VSTA code editor, and then click Stop Debugging on the Debug menu (or press Ctrl+Alt+Break).

 Hello World in Visual Studio with InfoPath Design Mode Integration

The following walkthrough shows how to write the same Hello World example as the previous walkthrough, but instead using Visual Studio 2005 with the InfoPath design mode integration enabled by installing Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, or Visual Studio 2008 with Visual Studio Tools for Office.Create a new project and specify the programming language (Visual Studio)

Start Visual Studio.
On the File menu, point to New, and then click Project.
Under Project types, expand the Visual C# folder, click Office (in Visual Studio 2008, then click 2007), and then click InfoPath Form Template.
Alternatively, you can expand the Other Languages folder, expand the Visual Basic folder, click Office (in Visual Studio 2008, then click 2007), and then click InfoPath Form Template.
Name the project HelloWorld, and then click OK.
In the Design a Form dialog box, click Form Template, click Blank, and then click OK.
The integrated InfoPath form template designer is displayed on a tab titled manifest.xsf [Design].
You are now ready to add a Button control and create its event handler.Add a Button control and event handler

In Design Tasks task pane, click Controls.
Note:
If the Design Tasks task pane is not visible, click Design Tasks on the View menu.
Drag a Button control from the Toolbox onto the form.
Double-click the Button control, type Hello for the Label property, and then click Edit Form Code. When prompted, save the form.
This will open the code editor window with the cursor in the event handler for the Clicked event of Button control.
You are now ready to add form code to the event handler for the button.Add “”Hello World”” code to the event handler and preview the form

In the event hander skeleton, type:

C#
Copy Code
MessageBox.Show(“”Hello World!””);

Visual Basic
Copy Code
MessageBox.Show(“”Hello World!””)

The code for your form template should look similar to the following:

C#
Copy Code
using Microsoft.Office.InfoPath;using System;using System.Windows.Forms;using System.Xml;using System.Xml.XPath;namespace HelloWorld {public partialclass FormCode {publicvoid InternalStartup() { ((ButtonEvent)EventManager.ControlEvents[“”CTRL1_5″”]).Clicked +=new ClickedEventHandler(CTRL1_5_Clicked); }publicvoid CTRL1_5_Clicked(object sender, ClickedEventArgs e) { MessageBox.Show(“”Hello World!””); } } }

Visual Basic
Copy Code
Imports Microsoft.Office.InfoPathImports SystemImports System.Windows.FormsImports System.XmlImports System.Xml.XPathNamespace HelloWorldPublicClass FormCodePrivateSub InternalStartup(ByVal senderAsObject,ByVal eAs EventArgs)HandlesMe.StartupAddHandlerDirectCast(EventManager.ControlEvents(“”CTRL1_5″”), ButtonEvent).Clicked,AddressOf CTRL1_5_ClickedEndSubPublicSub CTRL1_5_Clicked(ByVal senderAsObject,ByVal eAs ClickedEventArgs) MessageBox.Show(“”Hello World!””)EndSubEndClassEndNamespace

On the Debug menu, click Start Debugging (or press F5).
The InfoPath Preview window is displayed.
Click the Hello button.
A message box will be displayed with the text “”Hello World!””
Click OK, and then click the Close Preview button on the Standard toolbar to return to Visual Studio.
The next procedure shows how to add debugging breakpoints to your form code.Debug form code

Click the FormCode.cs or FormCode.vb tab to return to the code editor.
Click the grey bar to the left of the line:

C#
Copy Code
MessageBox.Show(“”Hello World!””);

Visual Basic
Copy Code
MessageBox.Show(“”Hello World!””)

A red circle is displayed and the line of code is highlighted to indicate that the runtime will pause at this breakpoint in your form code.
On the Debug menu, click Start Debugging (or press F5).
In the InfoPath Preview window, click the Hello button.
The code editor is given focus, and the breakpoint line is highlighted.
On the Debug menu, click Step Over (or press F10) to continue stepping through the code.
The event handler code is executed, and the “”Hello World!”” message is displayed.
Click OK to return to the code editor, and then click Stop Debugging on the Debug menu (or press Ctrl+Alt+Break).

 Getting the Current User’s Name

By using the .NET Framework classes, you can get access to additional functionality that is not easily available when working with business logic written in script. In the following example, you will learn how to use the UserName property of the Environment class to retrieve the name of the current user and populate the value of a Text Box control using an event handler for theLoading [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.formevents.loading(VS.80).aspx ] event.
Populating the Text Box control is accomplished by using two instances of theXPathNavigator [ http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator(VS.80).aspx ] class.
The first instance uses theMainDataSource [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.xmlform.maindatasource(VS.80).aspx ] property of theXmlForm [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.xmlform(VS.80).aspx ] class to retrieve an instance of theDataSource [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.datasource(VS.80).aspx ] class representing the underlying XML document of the form. This instance of the DataSource class then uses theCreateNavigator [ http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.datasource.createnavigator(VS.80).aspx ] method to position the XPathNavigator at the root node of XML document.
The second instance of the XPathNavigator is positioned to the employee field in the XML document by using the first instance, and then the SetValue method of the XPathNavigator class is used to set the value of the field with the UserName property.
For more information on working with System.Xml in managed code form templates, seeHow to: Work with the XPathNavigator and XPathNodeIterator Classes [ http://msdn.microsoft.com/en-us/library/aa945227(VS.80).aspx ] .Add a Loading event handler

Open the HelloWorld project that you created in the previous walkthrough.
If you are working in InfoPath with Microsoft Visual Studio Tools for Applications (VSTA), start InfoPath and open the form in design mode in InfoPath. If you are working with Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System or Visual Studio 2008 with Visual Studio Tools for Office, start Visual Studio and open the project.
On the View menu, click Data Source.
Right click the myFields folder, and then click Add.
In Name, type employee, and then click OK.
Drag the employee field onto the view.
If you are working in InfoPath, on the Tools menu, click Programming, and then click Loading Event.
If you are working in Visual Studio, on the Insert menu, click Event, and then click Loading Event.
This will create an event handler for the Loading event, and move the focus to that event handler in the code editor.
In the code editor, type the following:

C#
Copy Code
publicvoid FormEvents_Loading(object sender, LoadingEventArgs e) { System.Xml.XPath.XPathNavigator root, user; root =this.MainDataSource.CreateNavigator(); user = root.SelectSingleNode(“”//my:employee””,this.NamespaceManager); user.SetValue(System.Environment.UserName); }

Visual Basic
Copy Code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s