Section 10.2.  A simple sample script

Prev don't be afraid of buying books Next

10.2. A simple sample script

Let's start with a very simple example to show the basics of script creation. To implement a simple script that displays a message for the user:

1. Start with a blank form, by clicking New Blank Form on the Design a Form task pane.

2. Click Controls on the Design Tasks task pane.

3. Drag a Button from the list of controls to the blank form.

4. Right-click the button in the form and click Button Properties. This will bring up the Button Properties dialog shown in Figure 10-1. From this dialog, you can change the label displayed on the button, as well as the associated script ID, which is by default assigned a unique name like CTRL1_5.

Figure 10-1. The Button Properties dialog




5. Click Microsoft Script Editor. This will bring up Script Editor, which is the software used to create and edit script functions. Near the bottom of the script file (shown in Figure 10-2) is a function named something like CTRL1_5::OnClick, where CTRL1_5 is the button's name. The comment //Write your code here is displayed as a prompt.

Figure 10-2. Script Editor




6. Type the following line into the script body: XDocument.UI.Alert("This is your first script")

7. Save the script by clicking Save on the File menu in Script Editor, and exit Script Editor.

8. Save the form itself by clicking Save on the File menu in InfoPath.

Now, test your script by filling out the form you just created. The easiest way to do this is to click Preview Form on the toolbar. When clicking the Button button, you should get a dialog saying "This is your first script", as shown in Figure 10-3.

Figure 10-3. Output of the simple script




Let's take a closer look at the script in the example. The name of the function into which you inserted your code is something like CTRL1_5::OnClick.CTRL1_5 is the script ID of the button, and OnClick is the name of the event that causes that function to be executed. We will look more closely at events in section 10.4, "Events", on page 232.

The single line of code refers to the object XDocument, which represents the current document that is displayed in the form. It then refers to the document's user interface via the property UI. Finally, it calls the Alert method of that UI object, passing it one parameter that is the string "This is your first script". The Alert method is used to display a message.

Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net