Looping over controls in a TabControl

  • Thread starter Thread starter W
  • Start date Start date
W

W

Hi,
The code below only returns the tabs. I am trying to return the
control on each tab. How can I do that?

Public Sub ClearForm(ByVal CallingForm As Form)
Dim c, tc As Control

For Each c In CallingForm.Controls
If TypeOf c Is TabControl Then
For Each tc In c.Controls
If TypeOf tc Is TextBox Then
tc.Text = ""
ElseIf TypeOf tc Is ComboBox Then
tc.Text = ""
ElseIf TypeOf tc Is RadioButton Then
CType(tc, RadioButton).Checked = False
ElseIf TypeOf tc Is CheckBox Then
CType(tc, CheckBox).Checked = False
End If
Next
End If
Next
End Sub
 
Hi,
The code below only returns the tabs. I am trying to return the
control on each tab. How can I do that?

Public Sub ClearForm(ByVal CallingForm As Form)
Dim c, tc As Control

For Each c In CallingForm.Controls
If TypeOf c Is TabControl Then
For Each tc In c.Controls
If TypeOf tc Is TextBox Then
tc.Text = ""
ElseIf TypeOf tc Is ComboBox Then
tc.Text = ""
ElseIf TypeOf tc Is RadioButton Then
CType(tc, RadioButton).Checked = False
ElseIf TypeOf tc Is CheckBox Then
CType(tc, CheckBox).Checked = False
End If
Next
End If
Next
End Sub

Code not tested:

Public Sub ClearControls(ByVal tc As Control)
If TypeOf tc Is TextBox Then
tc.Text = ""
ElseIf TypeOf tc Is ComboBox Then
tc.Text = ""
ElseIf TypeOf tc Is RadioButton Then
CType(tc, RadioButton).Checked = False
ElseIf TypeOf tc Is CheckBox Then
CType(tc, CheckBox).Checked = False
End If

If tc.HasChildren
For Each ctl As Control In tc.Controls
ClearControls(ctl)
Next
End If
End Sub
 
For tab controls you need to iterate through each page (TypeOf is TabPage).
 
Hi,

AFAIK can you only do something on a tabcontrol as that is focus.

I never did this but I would in your case add a tabpage.select in the code
for every tabpage.

Cor
 
Code not tested:

Public Sub ClearControls(ByVal tc As Control)
        If TypeOf tc Is TextBox Then
            tc.Text = ""
        ElseIf TypeOf tc Is ComboBox Then
            tc.Text = ""
        ElseIf TypeOf tc Is RadioButton Then
            CType(tc, RadioButton).Checked = False
        ElseIf TypeOf tc Is CheckBox Then
            CType(tc, CheckBox).Checked = False
        End If

        If tc.HasChildren
          For Each ctl As Control In tc.Controls
            ClearControls(ctl)
          Next
       End If
End Sub- Hide quoted text -

- Show quoted text -

The reason I nested the code is that the clearForm code does not see
any controls that are on the TabControl.
 
Hi,

AFAIK can you only do something on a tabcontrol as that is focus.

I never did this but I would in your case add a tabpage.select in the code
for every tabpage.

The question is, how can I access a control that is in the tabpage. I
couldn't figure that out. I assumed that it would be
"tabControl.controls"
 
This code will clear text boxes on a form that has a tabcontrol..

For Each c As Control In Me.Controls
If TypeOf c Is TextBox Then
c.Text = ""
End If
If TypeOf c Is TabControl Then
For Each tp As Control In c.Controls
If TypeOf tp Is TabPage Then
For Each tb As Control In tp.Controls
If TypeOf tb Is TextBox Then
tb.Text = ""
End If
Next
End If
Next
End If
Next

On Feb 10, 2:47 am, "Cor Ligthert[MVP]" <[email protected]>
wrote:

snip

The question is, how can I access a control that is in the tabpage. I
couldn't figure that out. I assumed that it would be
"tabControl.controls"
 
The reason I nested the code is that the clearForm code does not see
any controls that are on the TabControl.

I don't understand your reply. Did you look at or try my code? It
recursively calls itself to find all child controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top