Problem: You have a drop-down box list box control on an InfoPath form. The drop-down list box contains manually entered items, which means that the values and display names are part of the view the drop-down list box is located on. You want to programatically retrieve these values and display names when the form opens.
You can use the OpenFileFromPackage() method of the FormTemplate object of a form to access the XSL file that is used to create controls on a specific view
To retrieve design information for a control on a view:
1 - Create a new InfoPath template and add a Drop-Down List Box control to View1
2 - Add the manual choices to Drop-Down List Box Control:
Value: 1
Display Name: Item 1
Do the same for items 2, 3, 4 & 5.
3 - Add the following code to the event handle of the Loading event of the form
Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As LoadingEventArgs)
' Write your code here.
Using stream As System.IO.Stream = Template.OpenFileFromPackage("View1.xsl")
If stream Is Nothing Then
Return
Else
If stream.Length = 0 Then
Return
End If
End If
Dim doc As XPathDocument = New XPathDocument(stream)
Dim root As XPathNavigator = doc.CreateNavigator
Dim dropdown As XPathNavigator = root.SelectSingleNode("//*[@xd:xctname = 'dropdown' and @xd:binding = 'my:field1']", NamespaceManager)
Dim iter As XPathNodeIterator = dropdown.Select("//option", NamespaceManager)
If iter IsNot Nothing Then
While iter.MoveNext
Dim value As String = String.Empty
Dim displayname As String = String.Empty
Dim valuenode As XPathNavigator = iter.Current.SelectSingleNode("@value", NamespaceManager)
If valuenode IsNot Nothing Then
value = valuenode.Value
End If
Dim iter2 As XPathNodeIterator = iter.Current.SelectChildren(XPathNodeType.Text)
iter2.MoveNext()
displayname = iter2.Current.Value
If displayname = "Item 4" Then
MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field1", NamespaceManager).SetValue(value)
End If
End While
End If
stream.Close()
End Using
End Sub
4 - Save and build the project
When the form opens, Item 4 should be displayed as the selected item in the drop-down list box.
The OpenFileFromPackage() method of the FormTemplate object of a form allows you to open fiels that are stored in the form template (.xsn).
Once you have an XPathDocument object, you can use this XPathDoucment object to read the data from the XSL file and retrieve the design information for any control.
No comments:
Post a Comment