Microsoft KB Archive/826997

= How to programmatically change the default view of an InfoPath form =

Article ID: 826997

Article Last Modified on 4/30/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

-



IN THIS TASK

 * SUMMARY
 * Create a New Form
 * Create View 1
 * Create View 2
 * Add the Script
 * Try It Out



SUMMARY
Microsoft Office InfoPath forms support multiple views. One view is the default view. When you open a form in InfoPath, you open the form in the default view. This article describes how to programmatically change the default view of an InfoPath form.

back to the top

Create a New Form

 * 1) Start InfoPath.
 * 2) In InfoPath 2007, click Design a Form Template on the File menu.

In InfoPath 2003, click Design a Form on the File menu.
 * 1) In InfoPath 2007, click Blankin the Design a Form Template task pane, and then click OK.

In InfoPath 2003, click New Blank Form in the Design a Form task pane.

back to the top

Create View 1

 * 1) On the blank form, type This is View 1 on the first line, and then type Check to display View 2 when the form is opened on the second line.
 * 2) On the Task Pane drop-down menu, click Controls.
 * 3) In the Controls task pane, click Check Box.
 * 4) Change the Check Box text to View 2.

back to the top

Create View 2

 * 1) On the Task Pane drop-down menu, click Views.

View 1 is the default view for the form.
 * 1) In Actions section of the task pane, click Add a New View.
 * 2) Type the name View 2, and then click OK.
 * 3) On the blank form, type This is View 2.

back to the top

InfoPath 2007
In InfoPath 2007, you must first set the programming language that you want to use. Then, add the script.

Note This example uses the C# programming language. You can use Visual Basic or other available options.

Set the programming language
Before you run this sample script in InfoPath 2007, you must change the programming language to C#. To do this, follow these steps:

Warning The Remove Code command that is mentioned in the following steps removes all existing code from the current form. Therefore, do not use this option, unless you are sure that you want to perform this action.
 * 1) On the Tools menu, click Form Options.
 * 2) In the Category list, click Programming . If the Form template code language box is disabled, click Remove Code to remove all existing code in the form. If the Form template code language box is available, go to the next step.
 * 3) In the Form template code language box, click C#, and then click OK.

Use the script
Add the script to the Loading Event. To do this, follow these steps:  On the Tools menu, point to Programming, and then click Loading Event. Microsoft Visual Studio Tools for Applications starts.  Add the following code example to the Age_OnValidate event. {           //Create an XPathNavigator object for the main DOM XPathNavigator xnDocument = this.MainDataSource.CreateNavigator;

//Create an XPathNavigator object for field1 - the check box for switching the View XPathNavigator xnDefaultView = xnDocument.SelectSingleNode(&quot;/my:myFields/my:field1&quot;, this.NamespaceManager); //Insure the XPathNavigator object is not null or an empty string if ((xnDefaultView != null) && (xnDefaultView.Value != &quot;&quot;))

{               //Switch to the appropriate View switch (xnDefaultView.Value)

{                   case &quot;false&quot;: e.SetDefaultView(&quot;View 1&quot;); break;

case &quot;true&quot;: e.SetDefaultView(&quot;View 2&quot;); break;

}  Save the changes, and then close Visual Studio Tools for Applications. Save your form template as SwitchDefaultView.xsn, and then close the form template.

InfoPath 2003
To add the script in InfoPath 2003, follow these steps:  On the Tools menu, point to Script, and then click Microsoft Script Editor.  Add the following code to the Code window: function XDocument::OnLoad(eventObj) {   //Pick the default view based on the value of element DefaultView var objDefaultView = XDocument.DOM.selectSingleNode(&quot;//my:field1&quot;);

if ((objDefaultView != null) && (objDefaultView.text != &quot;&quot;)) {       switch (objDefaultView.text) {               case &quot;false&quot;: XDocument.ViewInfos(&quot;View 1&quot;).IsDefault = true; break; case &quot;true&quot;: XDocument.ViewInfos(&quot;View 2&quot;).IsDefault = true; break; }   } } </li> Save the script, and then close the Microsoft Script Editor.</li> Save your form template as SwitchDefaultView.xsn, and then close the form template.</li></ol>

back to the top

Try It Out

 * 1) On the File menu, click Fill Out a Form.

Note Make sure that you fill out the form in this step instead of previewing the form. Preview does not reflect any programmatic change of the default view in the OnLoad event. Preview mode always shows the view that is currently selected in the InfoPath Designer.
 * 1) In InfoPath 2007, click SwitchDefaultView under Open a Form.

In InfoPath 2003, click SwitchDefaultView in the task pane.

The form opens in View 1 (the initial default view).
 * 1) Click the View 2 check box.
 * 2) Save the form as SwitchDefaultViewForm.xml, and then close the form.
 * 3) On the File menu, click Open.
 * 4) Locate SwitchDefaultViewForm.xml, and then click Open.

The form opens in View 2.

back to the top

Additional query words: IP2007

Keywords: kbhowto KB826997

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.