Finding controls on a panel

A

Aaron

Is there a way to get the names of all of the textboxes that are placed on a
panel?

I have several panels on my .NET application, each with several textbox
controls. I would like to get all of the names of the textboxes and clear
to text values without manually typing out all of the names. Is there an
easy way to specify the panel that these controls are placed in and get
those names?

something like

for each textbox on panel1
textbox.value =""
next

Thanks,
Aaron
 
A

Aaron

If I use this code:

Sub BetaClearTextBoxes(ByVal PanelName As Panel)
Dim txb As Control
For Each txb In PanelName.Controls
If TypeOf txb Is TextBox Then
txb.text = ""
End If
Next
End Sub

Intellisense gives me an error stating that "text" is not a member of
"Control".
If I use this code (Dim txb as textbox instead of control)

Sub BetaClearTextBoxes(ByVal PanelName As Panel)
Dim txb As TextBox
For Each txb In PanelName.Controls
If TypeOf txb Is TextBox Then
txb.text = ""
End If
Next
End Sub

I get Specified cast is not valid.
This errors out on the for each txb line.

Any ideas?

Thanks,
Aaron
 
A

Aaron

Yeas, this code works:
Sub BetaClearTextBoxes(ByVal PanelName As Panel)
Dim txb As Control
For Each txb In PanelName.Controls
If TypeOf txb Is TextBox Then
DirectCast(txb, TextBox).Text = ""
End If
Next
End Sub

Thanks for the help! I had never heard of DirectCast...

Aaron
 
C

Cor Ligthert

Aaron

You are there almost
for each textbox on panel1
textbox.value =""
next
for each txb as control on panel1.controls
if typeof txb Is textbox then
txb.text = ""
end if
next

I hope this helps?

Cor
 
J

Jeff Johnson [MVP: VB]

Aaron said:
If I use this code:

Sub BetaClearTextBoxes(ByVal PanelName As Panel)
Dim txb As Control
For Each txb In PanelName.Controls
If TypeOf txb Is TextBox Then
txb.text = ""
End If
Next
End Sub

Intellisense gives me an error stating that "text" is not a member of
"Control".

Try

DirectCast(txb, TextBox).Text = ""
 
C

Cor Ligthert

brrrrrrrrrr I typed it direct in the message, this error I make seldom

And I knew I was missing something.

:)

Thanks

Cor
 
C

Cor Ligthert

Aaron,

I first was thinking I was making a quick typing mistake, however it can not
give an error.

Textbox derives text from control and therefore casting is not necessary.
So text is a property from Control. So how can you get the message that text
is not a property from Control.

I am curious why you got this?

Cor
 
C

Cor Ligthert

Jeff,
We call that "air code."
It was no typing mistake, when I answered it was late in evening here, so I
was direct thinking on an error. However casting should not be necessary
here because Text is a property from Control and I stopped to type the
casting with that and did that automaticly as well in this message.

Cor
 
J

Jeff Johnson [MVP: VB]

It was no typing mistake, when I answered it was late in evening here, so I
was direct thinking on an error. However casting should not be necessary
here because Text is a property from Control and I stopped to type the
casting with that and did that automaticly as well in this message.

Hmmm, you're right. It looks like it should work. I'll test it when I get a
chance.
 
A

Aaron

Text is not a property of control. At least not in asp.net.

If I do :
Dim C as control
C.text does not exist

Aaron
 
H

Herfried K. Wagner [MVP]

* "Aaron said:
Text is not a property of control. At least not in asp.net.

ACK. Then I would suggest to cast to the type of the class that
provides the property.
 
C

Cor Ligthert

Aaron,

When in this newsgroup is not told it is a webform than it is by default a
windowform when there are 2 possibilities.

In your message you did not tell, not a member of "web.ui.control" however
even "control". A panel is as well on a windowform as on a webform.

When it is a webform, than you would do that casting to get the right
object.

However I am glad you messaged it because I did absolute not understand it,
and was not thinking about a webform, because I thought that it needs
something more on a webform. Now I saw that that more is not when it is on a
panel but when it is direct on the page.

:)

Cor
 

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

Top