Loop through controls to clear

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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 said:
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
 
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
 
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.
 
Back
Top