Problem with controls!!

Puiu

Regular
Joined
Oct 6, 2004
Messages
90
Hello everyone!
I have a Tab panel(named pnlInfo) on my form and on that panel i have 2 textboxes.
The following code should return the number of the textboxes (2) but it doesn't work..it returns 0!
Do u know why?


dim i as integer
Dim ctrl As Control


i = 0

For Each ctrl In pnlInfo.Controls
If TypeOf ctrl Is TextBox Then
i += 1
End If


Next
MessageBox.Show(CStr(i))
 
With only looking at your code, and making a quick project, it works fine. If those are the only controls on your panel, try the following just to make sure they are in the control collection of the panel.

Code:
dim i as integer
Dim ctrl As Control


i = 0

For Each ctrl In pnlInfo.Controls 
If TypeOf ctrl Is TextBox Then
i += 1
End If


Next
MessageBox.Show(CStr(i))
'Control Count of Panel
MessageBox.Show(pnlInfo.Controls.Count)


Now are you saying that there is a panel on a tab control, or just a panel control?
 
Sounds like a Tab Control. What you need to do is loop through the container control that contains the textboxes, whether it is a Tab Control page or a Panel or a GroupBox, etc.
 
try something like this. . .

Visual Basic:
Function CountControlsOfType(ByVal AControl As Control, ByVal aType As Type)[indent]Dim actrl As Control 

 
CountControlsOfType = 0
 
 
 
 


For Each actrl In AControl.Controls[indent]If actrl Is aType Then CountControlsOfType = CountControlsOfType + 1

 
 
CountControlsOfType = CountControlsOfType + CountControlsOfType(actrl, aType)
 
 

[/indent]Next actrl

 
 
 
 

[/indent]End Function
call it like this



i = CountControlsOfType(Panel1, Type.GetType("System.Windows.Forms.TextBox"))
 
Back
Top