[RESOLVED] For each control next loop
hi all,
i was trying to find a way to clear multiple textboxes.text in my form.
i found out about this code and tried it but having problems tracing textboxes only.
Code:
Private Sub Clear_Fields(ByVal CForm As Form)
Dim CControl As System.Windows.Forms.Control
Dim tb As System.Windows.Forms.TextBox
For Each CControl In CForm.Controls
CControl.Text = ""
Next
End Sub
unfortunately this code erases all text not only textboxes so i tried this
Code:
Private Sub Clear_Fields(ByVal CForm As Form)
Dim CControl As System.Windows.Forms.Control
Dim tb As System.Windows.Forms.TextBox
For Each CControl In CForm.Controls
If CControl.GetType Is tb.GetType Then
CControl.Text = ""
End If
Next
End Sub
this doesn't do anything unfortunately...
so then i tried this
Code:
Private Sub Clear_Fields(ByVal CForm As Form)
Dim tb As System.Windows.Forms.TextBox
For Each tb In CForm.Controls
tb.Text = ""
Next
End Sub
it didnt work as well so im out of ideas right now... any suggestions?
thanks.
Re: For each control next loop
Hi there,
For all of the controls that are directly on your form:
Code:
For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox Then
ctl.Text = String.Empty
End If
Next ctl
Re: For each control next loop
Quote:
Originally Posted by adshocker
hi all,
i was trying to find a way to clear multiple textboxes.text in my form.
i found out about this code and tried it but having problems tracing textboxes only.
Code:
Private Sub Clear_Fields(ByVal CForm As Form)
Dim CControl As System.Windows.Forms.Control
Dim tb As System.Windows.Forms.TextBox
For Each CControl In CForm.Controls
CControl.Text = ""
Next
End Sub
unfortunately this code erases all text not only textboxes so i tried this
Code:
Private Sub Clear_Fields(ByVal CForm As Form)
Dim CControl As System.Windows.Forms.Control
Dim tb As System.Windows.Forms.TextBox
For Each CControl In CForm.Controls
If CControl.GetType Is tb.GetType Then
CControl.Text = ""
End If
Next
End Sub
this doesn't do anything unfortunately...
so then i tried this
Code:
Private Sub Clear_Fields(ByVal CForm As Form)
Dim tb As System.Windows.Forms.TextBox
For Each tb In CForm.Controls
tb.Text = ""
Next
End Sub
it didnt work as well so im out of ideas right now... any suggestions?
thanks.
Hi,
You should do this:
Code:
For each Tb In Me.Controls
If tb Is TextBox then
tb.Text=""
End If
Next
Hope it helps,
sparrow1
Re: For each control next loop
thanks... both suggestions worked.
Re: For each control next loop
Quote:
Originally Posted by adshocker
thanks... both suggestions worked.
Hi,
Mark your thread as resolved then. :wave:
Wkr,
sparrow1
Re: For each control next loop
Glad to help. :)
Note that those are only for TextBoxes right on your form. If you have TextBoxes in containers that you want to clear, you'll need to do it a little differently.
Code:
Dim ctl As Control = Me.GetNextControl(Me, True)
Do Until ctl Is Nothing
If TypeOf ctl Is TextBox Then
ctl.Text = String.Empty
End If
ctl = Me.GetNextControl(ctl, True)
Loop
And don't forget to mark the thread resolved. ;)