473,795 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.val ue = False

Private Sub UnSelectOpts(By Val passedframeCapt ion As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Con tainer = passedframeCapt ion Then
thiscontrol.Val ue = False
end if
end if
Next
End sub

How can I clear the option buttons?
Jul 21 '05 #1
3 2584
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*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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.val ue = False
Private Sub UnSelectOpts(By Val passedframeCapt ion As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Con tainer = passedframeCapt ion Then
thiscontrol.Val ue = 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********@com cast.nospam.net > wrote in message
news:eD******** ******@TK2MSFTN GP09.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*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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.val ue = False

Private Sub UnSelectOpts(By Val passedframeCapt ion As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Con tainer = passedframeCapt ion Then
thiscontrol.Val ue = 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************ *******@tostops pamhotmail.com> wrote in message
news:pj******** ************@ne ws04.bloor.is.n et.cable.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********@com cast.nospam.net > wrote in message
news:eD******** ******@TK2MSFTN GP09.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*******@disc ussions.microso ft.com> wrote in message
news:50******** *************** ***********@mic rosoft.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.val ue = False

Private Sub UnSelectOpts(By Val passedframeCapt ion As String)
Dim thiscontrol As Control
For Each thiscontrol In Me 'Iterate through each element.
If TypeOf thiscontrol Is OptionButton Then
If thiscontrol.Con tainer = passedframeCapt ion Then
thiscontrol.Val ue = 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
10296
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 = ""; MessageBox.Show(c.Name.ToString());
3
2652
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 and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created I have to set
4
6377
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 page and how do you loop through the controls on the content page when the content is inside the ContentHolder control? Below is an example of the control loop before using a master page. Sub FindControlsInWebForm() Dim strValue As String = ""
6
3149
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
2528
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 forms (or panels), i have many controls (inherited controls from base controls : MyTextbox, MyCombobox ..). I add a new property on these controls : .BeginningGroup (boolean : yes or no).
7
4049
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 the loop. What is the correct way to go about this. I thought this was answered before but can not find it. TIA, John
3
351
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 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...
6
1570
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 on the form Dim controlOnTab As Control 'Places a control on the tab Dim controlTabPage As Control 'Places a control on the tab page Dim controlGroupBox As Control 'Places a control on the group box
3
11389
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) remove these items, but I noticed that not all of my buttons are getting removed from the screen. _The Code This is a sample of how I'm CREATING the buttons: for (int i = 0; i < arrButtonData.GetLength(0); i++) { //Make new button
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.