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

K

KKuser

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!!
 
X

Xian

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
 
J

Jorge

-----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 clear
them up at one time using the
statement "With..........End With".
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

K

KKuser

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 !!
 
C

Cor Ligthert

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
 
K

KKuser

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......
 
C

Cor Ligthert

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
///
 
K

KKuser

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
 
C

Cor Ligthert

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)
 
H

Herfried K. Wagner [MVP]

* "KKuser said:
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.
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top