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
taviafAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
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

0
taviafAuthor Commented:
@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!!!!!
0
Duy PhamFreelance IT ConsultantCommented:
@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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

taviafAuthor Commented:
@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
0
Duy PhamFreelance IT ConsultantCommented:
Could you post your real/full code here?

I did made a test (see attached file) and it worked just fine.
GetNextControltest.zip
0
taviafAuthor Commented:
@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

0
taviafAuthor Commented:
@Duy,
Thank you for everything. I found out that the reason that this didn't work for me was because of the multi-layers. Group box on top of group boxes. I needed a recursive function like this:


Private Sub saveit()

For Each cntrl As Control In GetAllControlsList(Me)
          'Search for a CheckBox
                    Dim ctlCheckBox As CheckBox
                    If TypeOf cntrl Is CheckBox Then
                        ctlCheckBox = cntrl
                        If ctlCheckBox.Checked Then
                            outputFile.wplWriteLine(ctlCheckBox.Name, "TRUE")
                        Else
                            outputFile.wplWriteLine(ctlCheckBox.Name, "")
                        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

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


 Function GetAllControlsList(ByVal aCtrl As Control) As List(Of Control)
        Dim ctrlList As New List(Of Control)

        For Each ctrl As Control In aCtrl.Controls
            If ctrl.HasChildren = True Then
                ' does not add containers, makes iterative call to get container's controls
                ctrlList.AddRange(GetAllControlsList(ctrl))
            Else
                ctrlList.Add(ctrl)
            End If
        Next

        Return ctrlList
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Duy PhamFreelance IT ConsultantCommented:
@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.
0
taviafAuthor Commented:
I was able to fix the problem and i would like to share the code as a solution....
0
taviafAuthor Commented:
@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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.