Link to home
Start Free TrialLog in
Avatar of Natavia Finnie
Natavia FinnieFlag for United States of America

asked on

GetNextControl is saving repeated data when used with for loop

Dim cntrl As Control
Dim I As Integer

For I = 0 To Me.tabControl.TabPages.Count-1
    cntrl = Me.tabControl.TabPages(I)
    Do
         cntrl = Me.GetNextControl(cntrl, True)
          If TypeOf cntrl Is TextBox Then
                  outputFile.wplWriteLine(cntrl.Name, Trim(cntrl.Text))
           End If
          ......other code here.....
   Loop While Not (cntrl Is Nothing) And (cntrl.Parent.Name <> Me.tabControl.TabPages(I).Name)
Next I

Open in new window

I am saving data from all my controls on the interface using the code here, which works great. One of my controls is a CheckedListBox which is located on the first tab.  This CheckedListBox show/hides other tabs. The problem is that it saves the first tab's data then moves on to the other tabs and saves the data per tab.  Then it loops back and continues to save the data again, which is shown in the attached file.

I tried using a uniqueTabNameList that adds the tab name and then continues to process the code if the tab name is NOT there...and that does not work either.

' the code below is entered after cntrl = Me.AllTabs.TabPages(i) and before the 'Do"

If Not tabNameList.Contains(cntrl.Name) Then
                       tabNameList.Add(cntrl.Name)

 ... embedded code here....

end if

Dim cntrl As Control
Dim I As Integer

For I = 0 To Me.tabControl.TabPages.Count-1
    cntrl = Me.tabControl.TabPages(I)
    Do
         cntrl = Me.GetNextControl(cntrl, True)
          If TypeOf cntrl Is TextBox Then
                  outputFile.wplWriteLine(cntrl.Name, Trim(cntrl.Text))
           End If
          ......other code here.....
   Loop While Not (cntrl Is Nothing) And (cntrl.Parent.Name <> Me.tabControl.TabPages(I).Name)
Next I

Open in new window

savingGeneral2.txt
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Since we know exact number of child controls in each tab, we can just use For Loop instead of Do While to iterate through all controls in each tab without having to worry about being repeated.

Sample code (tested):
Dim cntrl As Control
Dim I As Integer
Dim J As Integer

For I = 0 To Me.tabControl.TabPages.Count-1
    cntrl = Me.tabControl.TabPages(I)
    For J = 1 To Me.tabControl.TabPages(I).Count
         cntrl = Me.GetNextControl(cntrl, True)
         If TypeOf cntrl Is TextBox Then
               outputFile.wplWriteLine(cntrl.Name, Trim(cntrl.Text))
         End If
         ......other code here.....
    Next J
Next I

Open in new window

Avatar of Natavia Finnie

ASKER

@duy,
It is not doing anything. Nothing is saving. I had to change
Me.tabControl.TabPages(I).Count   to  Me.tabControl.TabPages(I).Count.Controls.Count-1 to make the eror message go away that I was receiving.


Help!!!!!
@taviaf:  Sorry, my bad copy/paste. There is an error line line 7, it should be Me.tabControl.TabPages(I).Controls.Count instead of Me.tabControl.TabPages(I).Count
Dim cntrl As Control
Dim I As Integer
Dim J As Integer

For I = 0 To Me.tabControl.TabPages.Count-1
    cntrl = Me.tabControl.TabPages(I)
    For J = 1 To Me.tabControl.TabPages(I).Controls.Count
         cntrl = Me.GetNextControl(cntrl, True)
         If TypeOf cntrl Is TextBox Then
               outputFile.wplWriteLine(cntrl.Name, Trim(cntrl.Text))
         End If
         ......other code here.....
    Next J
Next I

Open in new window

@Duy,
I had already caught that and fixed that error. I also added the Do-While loop and it is still not iterating through any of the controls. cntrl returns nothing so nothing is saving.....

It is not working with or without the Do-While loop
SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Duy,
I still get NOTHING.....I do believe that it works for you. It is just not working for what I am trying to do, for some reason. I have 13 tab pages with several group boxes per page. Each group box may have several different controls on them, including text boxes, combo boxes, datagrids, and/or other group boxes and more.  I am handling each control differently depended on type.

 For I = 0 To AllTabs.TabPages.Count - 1
          cntrl = AllTabs.TabPages(I)
          For J = 1 To AllTabs.TabPages(I).Controls.Count - 1
                    cntrl = Me.GetNextControl(cntrl, True)

                            ' Search for a TextBox
                            If TypeOf cntrl Is TextBox Then
                                outputFile.wplWriteLine(cntrl.Name, Trim(cntrl.Text))
                            End If

                            ' Search for a DateTimePicker
                            Dim ctlDate As DateTimePicker
                            If TypeOf cntrl Is DateTimePicker Then
                                ctlDate = cntrl
                                If Me.ProtocolReport.Checked Then
                                    outputFile.wplWriteLine(ctlDate.Name, ctlDate.Value.ToString("MMMM yyyy"))
                                ElseIf Me.FinalReport.Checked Then
                                    outputFile.wplWriteLine(ctlDate.Name, ctlDate.Value.ToString("MMMM dd, yyyy"))
                                End If
                            End If

                            'Search for a RadioButton
                            Dim ctlRadioBtn As RadioButton
                            If TypeOf cntrl Is RadioButton Then
                                ctlRadioBtn = cntrl
                                If ctlRadioBtn.Checked Then
                                    outputFile.wplWriteLine(ctlRadioBtn.Name, "TRUE")
                                Else
                                    outputFile.wplWriteLine(ctlRadioBtn.Name, "")
                                End If
                            End If
                   Next
          Next

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@taviaf; You're welcome :).
And since I didn't help to solve the issue, so it's totally fine if you just close the request without giving me any point.
I was able to fix the problem and i would like to share the code as a solution....
@Duy,
It was your assistance that led me to the following code that works with GetNextControl.  All we had to was set GetNextControl = TabPage.GetNextControl because it will get the next control of the tab page.  I kept setting GetNextControl = Me.GetNextControl and it would start with the form and move forward and that is why it kept giving me duplicates.  It was going back-and-forth. #TeamWork

 For Each TabPage As TabPage In Me.AllTabs.TabPages
                    Dim cntrl As Control = TabPage
                 
                    Do Until cntrl Is Nothing
                        cntrl = TabPage.GetNextControl(cntrl, True)
                
                   *****code here******
                    Loop
 Next

Open in new window