469,612 Members | 1,602 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

[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 28670
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> 在郵件
news:uS**************@TK2MSFTNGP12.phx.gbl 中撰寫...
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> 在郵件
news:u2*************@TK2MSFTNGP10.phx.gbl 中撰寫...
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> 在郵件
news:u2*************@TK2MSFTNGP10.phx.gbl 中撰寫...
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Martin Hazell | last post: by
3 posts views Thread by Steve B. | last post: by
4 posts views Thread by BradC | last post: by
4 posts views Thread by Patrick.O.Ige | last post: by
2 posts views Thread by CsaaGuy | last post: by
1 post views Thread by Leonardo Santos-Macias | last post: by
4 posts views Thread by Ravi Ambros Wallau | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.