Showing posts with label Controls. Show all posts
Showing posts with label Controls. Show all posts

Sunday, April 7, 2013

Retrieve a row of a repeating table

Problem: You have a repeating table on an InfoPath form and want to be able to retrieve the values of fields that are located in one specific row of the repeating table.

Solution:

You can retrieve rows of a repeating table either by their position within the repeating table or by the value of a specific field within the repeating table.

1 - Create a new InfoPath form

2  - Add a Repeating Table with 2 columns ("field1" & "field2")

3 - Add a Text Box control to the view and name it contents

4 - Add a second Text Box control to the view and name it toSelect

5 - Add a Button control and add the following code to the Clicked event handler:

A) If you want by position:


Public Sub CTRL14_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            If String.IsNullOrEmpty(toSelect) Then
                toSelect = "1"
            End If

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[" & toSelect & "]", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

            mainDS.SelectSingleNode("/my:myFields/my:Contents", NamespaceManager).SetValue(sb.ToString())


        End Sub

B) If you want by field value:

        Public Sub CTRL15_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[my:field1 = '" & toSelect & "']", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

            mainDS.SelectSingleNode("/my:myFields/my:Contents", NamespaceManager).SetValue(sb.ToString())

        End Sub


6 - Save and preview the form

If the form retrieves rows by position, enter a number in the toSelect text box and click on the button.

If the form retrieves rows by field value then enter a piece of the text that is the same as the value of one of the field1 fields in any row of the repeating table.

Loop through rows of a repeating table

Problem: You have a repeating table on an InfoPath form and want to sequentially retrieve each row of the repeating table

Solution:
You can use the XPathNodeIterator object to loop through rows of a repeating table.

1 - Create a new InfoPath form

2 - Add a Repeating Table control with 2 columns

3 - Add a Text Box control to the view of the form and name it contents. Select it's Multi-Line property.

4 - Add a Button control to the form and on it's Clicked Event Handler write:


Public Sub CTRL8_11_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.

            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim rows As XPathNodeIterator = mainDS.Select("/my:myFields/my:group3/my:group4", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()

            While rows.MoveNext

                sb.Append(rows.Current.SelectSingleNode("my:field4", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(rows.Current.SelectSingleNode("my:field5", NamespaceManager).Value)
                sb.AppendLine()
            End While

            mainDS.SelectSingleNode("/my:myFields/my:contents", NamespaceManager).SetValue(sb.ToString())

        End Sub


5 - Save and preview the form

When the form opens, add a couple of rows of data to the repeating table and then click on the button. The Text Box will display all data you entered into the fields in the Repeating table separated by semi-colons.

Saturday, February 2, 2013

Retrieve design information for a control on a view

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.

About Me

My photo
Toronto, Ontario, Canada
MBA, M.Sc. & MCP