473,399 Members | 2,278 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,399 software developers and data experts.

[VB.Net] To clear up all the textbox at one time

Hello:

I have 20, 30 text and combo boxes on one form, and I would like to clear
them up at one time using the statement "With..........End With".

I used this when using VB6, but I don't know if I can use the same way in
VB.Net. If I can't use With statement in VB.Net, what should I do?

Thanks!!
Nov 20 '05 #1
13 29090
Dim ctrl As Control
Dim txt As TextBox

For Each ctrl In Me.Controls
If (ctrl.GetType() Is GetType(TextBox)) Then
txt = CType(ctrl, TextBox)
txt.Text = ""
End If
Next
"KKuser" <a.*@c.d.com> ha scritto nel messaggio
news:O3**************@TK2MSFTNGP09.phx.gbl...
Hello:

I have 20, 30 text and combo boxes on one form, and I would like to clear
them up at one time using the statement "With..........End With".

I used this when using VB6, but I don't know if I can use the same way in
VB.Net. If I can't use With statement in VB.Net, what should I do?

Thanks!!

Nov 20 '05 #2
-----Original Message-----
Hi
I use the following procedures to clear the text.
They use System.Reflection

Kind Regards
Jorge

PS: Its in Portuguese ...
Limpa=clean,meuform=myform,campos=field...


Public Sub LimpaTextBoxes(ByVal f As Form)

Dim meuForm As Type = f.GetType()
Dim campos As FieldInfo() = meuForm.GetFields
(BindingFlags.Instance Or BindingFlags.NonPublic)
For Each campo As FieldInfo In campos
If campo.FieldType.Name.ToLower = "textbox"
Then
Dim t As TextBox = DirectCast
(campo.GetValue(f), TextBox)
t.Text = ""
End If
Next
End Sub
Public Sub LimpaComboBoxes(ByVal f As Form)
Dim meuForm As Type = f.GetType()
Dim campos As FieldInfo() = meuForm.GetFields
(BindingFlags.Instance Or BindingFlags.NonPublic)
For Each campo As FieldInfo In campos
If campo.FieldType.Name.ToLower = "combobox"
Then
Dim c As ComboBox = DirectCast
(campo.GetValue(f), ComboBox)
c.Text = ""
End If
Next

End Sub

Hello:

I have 20, 30 text and combo boxes on one form, and I would like to clearthem up at one time using the statement "With..........End With".
I used this when using VB6, but I don't know if I can use the same way inVB.Net. If I can't use With statement in VB.Net, what should I do?
Thanks!!
.

Nov 20 '05 #3
Hi KKuser,

It looks the same as the others, however it is not, it cleans also when a
textbox or a combobox is places in another control as by instance a
groupbox.

(I changed it from something else so watch typos)

I hope this helps?

Cor

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doclean(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
if typeof ctr Is textbox then
ctr.txt = ""
doSet(ctr)
elseif typeof ctr Is combobox then
ctr. 'cleaning up depends here if you use the itemarray or the
datasource
Next
End Sub

Nov 20 '05 #4
* "KKuser" <a.*@c.d.com> scripsit:
I have 20, 30 text and combo boxes on one form, and I would like to clear
them up at one time using the statement "With..........End With".
You cannot.
I used this when using VB6, but I don't know if I can use the same way in
VB.Net. If I can't use With statement in VB.Net, what should I do?


How did you do that with the 'With' statement in VB6?

<URL:http://dotnet.mvps.org/dotnet/samples/controls/downloads/EnumerateControls.zip>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
Hello all:

Thank you for your advises, I have solved my problem.

In fact, my biggest problem is that I put all the textboxes in a groupbox,
but I didn't "mention" it in my codes.

For Each ThisControl In Me.GroupBox3.Controls
^^^^^^^^^
I should have added this.......

And another question...
In the same case, I just want to clear the text of the comboboxes, but keep
the options contained in it (ie. still keep all items in "Combobox.Items").
I have tried

ThisControl.Text = ""
("thiscontrol" now means a combobox)

, but it doesn't work. How to do that??

Thanks again !!
Nov 20 '05 #6
Hello all:

Thank you for your advises, I have solved my problem.

In fact, my biggest problem is that I put all the textboxes in a groupbox,
but I didn't "mention" it in my codes.

For Each ThisControl In Me.GroupBox3.Controls
^^^^^^^^^
I should have added this.......

And another question...
In the same case, I just want to clear the text of the comboboxes, but keep
the options contained in it (ie. still keep all items in "Combobox.Items").
I have tried

ThisControl.Text = ""
("thiscontrol" now means a combobox)

, but it doesn't work. How to do that??

Thanks again !!
Nov 20 '05 #7
Hi KKuser,

Which code did you try, I wrote that the code I was providing was doing all
this you wrote in this message..

With the difference that it becomes for the combobox then of course

elseif typeof ctr Is combobox then
ctr.selectedindex = -1
end if

I do not understand that this does not work, can you tell what goes wrong?

Cor

Thank you for your advises, I have solved my problem.

In fact, my biggest problem is that I put all the textboxes in a groupbox,
but I didn't "mention" it in my codes.

For Each ThisControl In Me.GroupBox3.Controls
^^^^^^^^^
I should have added this.......

And another question...
In the same case, I just want to clear the text of the comboboxes, but keep the options contained in it (ie. still keep all items in "Combobox.Items"). I have tried

ThisControl.Text = ""
("thiscontrol" now means a combobox)

, but it doesn't work. How to do that??

Thanks again !!

Nov 20 '05 #8
Hello Cor:

Thank you for your reply!! My code is :

Dim ThisControl As Control
...........
...........
...........
If TypeOf ThisControl Is ComboBox Then
ThisControl.SelectedIndex = -1
End If

But the "ThisControl.SelectedIndex = -1" line is marked due to
"SelectedIndex is not a member of 'System.Windows.Forms.Control'"

That's why I said it doesn't work......

"Cor Ligthert" <no**********@planet.nl> ¦b¶l¥ó
news:uS**************@TK2MSFTNGP12.phx.gbl ¤¤¼¶¼g...
Hi KKuser,

Which code did you try, I wrote that the code I was providing was doing all this you wrote in this message..

With the difference that it becomes for the combobox then of course

elseif typeof ctr Is combobox then
ctr.selectedindex = -1
end if

I do not understand that this does not work, can you tell what goes wrong?

Cor

Thank you for your advises, I have solved my problem.

In fact, my biggest problem is that I put all the textboxes in a groupbox, but I didn't "mention" it in my codes.

For Each ThisControl In Me.GroupBox3.Controls
^^^^^^^^^
I should have added this.......

And another question...
In the same case, I just want to clear the text of the comboboxes, but

keep
the options contained in it (ie. still keep all items in

"Combobox.Items").
I have tried

ThisControl.Text = ""
("thiscontrol" now means a combobox)

, but it doesn't work. How to do that??

Thanks again !!


Nov 20 '05 #9
Hi KKuser,

I did it this time very quick and dirty I saw, sorry for this,
Take this routine it is really fine, it is a real recursive routine which
solves all problems with controls in controls and what is more, which you
see often solved in long lines of code.

Now I have tested it as a cleaner (It was a sample of setting a tooltip I
once made).

I hope it works for you as well?
Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doclean(Me)
End Sub
Private Sub doclean(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
If TypeOf ctr Is TextBox Then
ctr.Text = ""
ElseIf TypeOf ctr Is ComboBox Then
DirectCast(ctr, ComboBox).SelectedIndex = -1
End If
doclean(ctr)
Next
End Sub
///


Nov 20 '05 #10
Hi Cor:

Thank you for your reply, and I have all my problems (about this issue)
solved !!

The only one difference is that I don't use recursive function (will a
recursive function make a better performance?). My code is as following:

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
Dim ThisControl As Control
For Each ThisControl In Me.GroupBox3.Controls
If TypeOf ThisControl Is TextBox Then
ThisControl.Text = ""
End If
If TypeOf ThisControl Is ComboBox Then
DirectCast(ThisControl, ComboBox).SelectedIndex = -1
End If
Next
End Sub
"Cor Ligthert" <no**********@planet.nl> ¦b¶l¥ó
news:u2*************@TK2MSFTNGP10.phx.gbl ¤¤¼¶¼g...
Hi KKuser,

I did it this time very quick and dirty I saw, sorry for this,
Take this routine it is really fine, it is a real recursive routine which
solves all problems with controls in controls and what is more, which you
see often solved in long lines of code.

Now I have tested it as a cleaner (It was a sample of setting a tooltip I
once made).

I hope it works for you as well?
Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doclean(Me)
End Sub
Private Sub doclean(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
If TypeOf ctr Is TextBox Then
ctr.Text = ""
ElseIf TypeOf ctr Is ComboBox Then
DirectCast(ctr, ComboBox).SelectedIndex = -1
End If
doclean(ctr)
Next
End Sub
///

Nov 20 '05 #11
Hi KKuser,

When you want only the textboxes in the groupbox than your code performs
better of course.

However when you want To clear up all the textboxes at one time, than you
can try my code sample.

:-)

Cor

"> Thank you for your reply, and I have all my problems (about this issue)
solved !!

The only one difference is that I don't use recursive function (will a
recursive function make a better performance?). My code is as following:

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
Dim ThisControl As Control
For Each ThisControl In Me.GroupBox3.Controls
If TypeOf ThisControl Is TextBox Then
ThisControl.Text = ""
End If
If TypeOf ThisControl Is ComboBox Then
DirectCast(ThisControl, ComboBox).SelectedIndex = -1
End If
Next
End Sub
"Cor Ligthert" <no**********@planet.nl> ¦b¶l¥ó
news:u2*************@TK2MSFTNGP10.phx.gbl ¤¤¼¶¼g...
Hi KKuser,

I did it this time very quick and dirty I saw, sorry for this,
Take this routine it is really fine, it is a real recursive routine which solves all problems with controls in controls and what is more, which you see often solved in long lines of code.

Now I have tested it as a cleaner (It was a sample of setting a tooltip I once made).

I hope it works for you as well?
Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doclean(Me)
End Sub
Private Sub doclean(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
If TypeOf ctr Is TextBox Then
ctr.Text = ""
ElseIf TypeOf ctr Is ComboBox Then
DirectCast(ctr, ComboBox).SelectedIndex = -1
End If
doclean(ctr)
Next
End Sub
///


Nov 20 '05 #12
* "KKuser" <a.*@c.d.com> scripsit:
Dim ThisControl As Control
..........
..........
..........
If TypeOf ThisControl Is ComboBox Then
ThisControl.SelectedIndex = -1
End If

But the "ThisControl.SelectedIndex = -1" line is marked due to
"SelectedIndex is not a member of 'System.Windows.Forms.Control'"


Seems that you are working with 'Option Strict On'.

You will have to cast:

\\\
If TypeOf ThisControl Is ComboBox Then
DirectCast(ThisControl, ComboBox).SelectedIndex = -1
End If
///

If it's a databound control, you will have to set 'SelectedIndex' to -1
twice.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #13
Hi Herfried,

Good addition this time, however I think it is better to tell that it are
additions.
If it's a databound control, you will have to set 'SelectedIndex' to -1
twice.


Cor
Nov 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Hazell | last post by:
For various reasons, I have had to produce a quick (!) page to edit one column of data in a database with ASP.net. With this being my first foray into ASP.net, I apoligise for any basic erros I have...
3
by: Steve B. | last post by:
Using: VS dB: MS-Access Although the the DataGrid displays the proper date from an Access file (Date/Time DataType) an adjacent bound textbox for that same DataGrid field displays the date and...
4
by: BradC | last post by:
We have a Windows 2000 web server (all patches up to date) that runs a variety of sites, most of which are straight HTML or ASP. We have recently added a couple of new ASP.NET sites that use...
4
by: Patrick.O.Ige | last post by:
Whats the best way to clear textboxes? Any ideas?
2
by: CsaaGuy | last post by:
Hi, I created a class in ap.net using vb.net that inherits from Textbox. I added a few of my own properties and methods, set it up to appear in the toolbox. And have used it. My properties that...
1
by: jason | last post by:
I've seen a few posts on this issue, but no clear solutions. I have a mulitiline textbox inside a datagrid. I use TemplateColumn to define as multiline with 3 rows. I have other field types...
1
by: MR | last post by:
Hi, it has been observed that when we try to instantiate a .Net dll for the first time in a session (e.g. from an exe in .Net), it takes almost 10 times compared to subsequent instantiations. ...
1
by: Leonardo Santos-Macias | last post by:
I have an asp.net textbox with some EULA and a button to accept the EULA. The textbox will have scrollbars since the EULA is several pages. Is there any way using asp.net, vbscript or jave to...
4
by: Ravi Ambros Wallau | last post by:
Hi: We developed a set of ASP.NET Web Applications that never runs in stand-alone mode, but always inside a portal (Rainbow Portal). All modules are copied on that portal. My question is: load...
2
by: g7murali123 | last post by:
hi, my textbox contains text as "search" on pageload when the user click the textbox the text "search" should disappear and the textbox must get the value of the user entering text. please help the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.