|
-
Jul 28th, 2007, 11:32 AM
#1
Thread Starter
Hyperactive Member
[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.
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Jul 28th, 2007, 11:34 AM
#2
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
-
Jul 28th, 2007, 11:36 AM
#3
Re: For each control next loop
 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
-
Jul 28th, 2007, 11:39 AM
#4
Thread Starter
Hyperactive Member
Re: For each control next loop
thanks... both suggestions worked.
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Jul 28th, 2007, 11:45 AM
#5
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.
-
Jul 28th, 2007, 11:45 AM
#6
Re: For each control next loop
 Originally Posted by adshocker
thanks... both suggestions worked.
Hi,
Mark your thread as resolved then.
Wkr,
sparrow1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|