473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Loop through controls to clear

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
3 2550
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thonglao Rud | last post by:
I'm trying to clear all textbox on the form. foreach (Control c in this.Controls) { //if (c.GetType() == typeof(TextBox)) if (c is TextBox) { // Found it c.Text = "";...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
4
by: sck10 | last post by:
I changed my aspx page to use a master page. The problem is that I can no longer loop through the controls on the content page. My question is how do you loop through the controls on the master...
6
by: Paul D. Fox | last post by:
I want to be able to loop through all the TextBoxes on a page and clear their values. How can I write a function to do that?
8
by: dominique | last post by:
Hi, Is it possible (in vb.net with WinForms) to loop throw controls inside a container (form or panel) sorting the controls on a property (.tabindex for example) ? My problem : on several...
7
by: J L | last post by:
I need to loop through a form's controls collection and delete some based on type and location. I have tried For..Each and For i = 0 to form.controls.count - 1 but when I delete any it messes up...
3
by: Joe | last post by:
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...
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
3
AHayes
by: AHayes | last post by:
_Background I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.