By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

Loop through controls to clear

P: n/a
Joe
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an example from my VB6 code that loops through controls in a specific frame on a form and unselects the option buttons. .NET barks at this line of code: thiscontrol.value = False

Private Sub UnSelectOpts(ByVal passedframeCaption As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Container = passedframeCaption Then
thiscontrol.Value = False
end if
end if
Next
End sub

How can I clear the option buttons?
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here's one way Private Sub Clear()

Dim opt as New Control

For Each opt in myForm.Controls
If opt.GetType = Forms.TextBox then DirectCast(opt, TextBox).Text =
String.Empty
Next
End Sub

But remember that some controls on the form are containers like Panels and
GroupBoxs.. http://www.knowdotnet.com/articles/thedotnetway.html

You can modify the sub though and call it recursively so you don't have to
worry about calling a seperate sub for each container.
"Joe" <an*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an
example from my VB6 code that loops through controls in a specific frame on
a form and unselects the option buttons. .NET barks at this line of code:
thiscontrol.value = False
Private Sub UnSelectOpts(ByVal passedframeCaption As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Container = passedframeCaption Then
thiscontrol.Value = False
end if
end if
Next
End sub

How can I clear the option buttons?

Jul 21 '05 #2

P: n/a
There is no "complete list of" controls in dot.net. You will have to do
this recusively since if your controls are parented by a panel lets say, the
panel would show in the first level. You would then have to iterate through
all controls on the panel and so on.

Lloyd Sheen

"William Ryan [eMVP]" <do********@comcast.nospam.net> wrote in message
news:eD**************@TK2MSFTNGP09.phx.gbl...
Here's one way Private Sub Clear()

Dim opt as New Control

For Each opt in myForm.Controls
If opt.GetType = Forms.TextBox then DirectCast(opt, TextBox).Text =
String.Empty
Next
End Sub

But remember that some controls on the form are containers like Panels and
GroupBoxs.. http://www.knowdotnet.com/articles/thedotnetway.html

You can modify the sub though and call it recursively so you don't have to
worry about calling a seperate sub for each container.
"Joe" <an*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
I'm wondering how to loop through controls in VB.NET. I have the code
from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an
example from my VB6 code that loops through controls in a specific frame on a form and unselects the option buttons. .NET barks at this line of code:
thiscontrol.value = False

Private Sub UnSelectOpts(ByVal passedframeCaption As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Container = passedframeCaption Then
thiscontrol.Value = False
end if
end if
Next
End sub

How can I clear the option buttons?


Jul 21 '05 #3

P: n/a
I agree entirely. If you only have one container you can just walk through
it but in most instances, the recursive method is the way to go. The reason
I mention the containers is b/c I've had a few occassions where I only
wanted perform things on controls with a given container control but this is
not all that common so recursively walking the form is the preferred method
for 'clear all' functionality. My apologies for any ambiguity.
"Lloyd Sheen" <sq*******************@tostopspamhotmail.com> wrote in message
news:pj********************@news04.bloor.is.net.ca ble.rogers.com...
There is no "complete list of" controls in dot.net. You will have to do
this recusively since if your controls are parented by a panel lets say, the panel would show in the first level. You would then have to iterate through all controls on the panel and so on.

Lloyd Sheen

"William Ryan [eMVP]" <do********@comcast.nospam.net> wrote in message
news:eD**************@TK2MSFTNGP09.phx.gbl...
Here's one way Private Sub Clear()

Dim opt as New Control

For Each opt in myForm.Controls
If opt.GetType = Forms.TextBox then DirectCast(opt, TextBox).Text =
String.Empty
Next
End Sub

But remember that some controls on the form are containers like Panels and GroupBoxs.. http://www.knowdotnet.com/articles/thedotnetway.html

You can modify the sub though and call it recursively so you don't have to worry about calling a seperate sub for each container.
"Joe" <an*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
I'm wondering how to loop through controls in VB.NET. I have the code from
VB6 ok, but I can't figure out how to do it correctly in .NET. This is

an example from my VB6 code that loops through controls in a specific frame

on
a form and unselects the option buttons. .NET barks at this line of code: thiscontrol.value = False

Private Sub UnSelectOpts(ByVal passedframeCaption As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Container = passedframeCaption Then
thiscontrol.Value = False
end if
end if
Next
End sub

How can I clear the option buttons?



Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.