| | |
code to clear all textboxes in a form
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
I am trying to find a code to clear all text boxes in a form. I am using VB8
Here is the code I found but I get an error telling me I need a comma between two arguments
Thanks
Here is the code I found but I get an error telling me I need a comma between two arguments
VB.NET Syntax (Toggle Plain Text)
Private Sub btnclearall_Click Dim ctl As New Control For Each ctl In Me.Controls Next If Typeof ctl Is textbox Then Dim txtcontrol As Textbox = Directcast (ctl.Textbox) Me.text = StringEmpty
Last edited by peter_budo; 19 Days Ago at 5:31 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 2
0
#2 23 Days Ago
Hello,
Try this
Try this
VB.NET Syntax (Toggle Plain Text)
For Each _control As Control In Me.Controls If TypeOf (_control) Is TextBox Then _control.Text = String.Empty End If Next
Last edited by peter_budo; 19 Days Ago at 5:31 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 1
Hi there,
Please try this,
Please try this,
VB.NET Syntax (Toggle Plain Text)
Call ClearContent()
VB.NET Syntax (Toggle Plain Text)
Private Sub ClearContent() TxtBoxStatus.Text = "" End sub
•
•
Join Date: Sep 2009
Posts: 288
Reputation:
Solved Threads: 37
0
#4 20 Days Ago
VB Syntax (Toggle Plain Text)
Private Sub btnclearall_Click Dim ctl As New Control For Each ctl In Me.Controls Next 'Ok above you looped through all controls on your form but did 'nothing with each itteration 'Now below is only checking one single control, the last one of your form If Typeof ctl Is textbox Then 'Above you detemined ctl, is a texbox, so why are 'you creating a new text box and converting the 'the existing textbox into another textbox? Dim txtcontrol As Textbox = Directcast (ctl.Textbox) 'Me is the form you are in, so the only thing 'you are changing is the text propert of the form itself 'not any of the text boxes. Me.text = StringEmpty[/COLOR]
See Rogachev's post above which shows how to correctly itterate thru the controls and clear the textboxes as it goes. But as an additional note, the coding provided will not itterate thru child controls within a container control. For example if you have a few panels and/or group boxes you would need to itterate thru each seperatly to find each of the child controls.
Last edited by TomW; 20 Days Ago at 9:30 am.
0
#5 20 Days Ago
You can use Extension Method - Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
VB.NET Syntax (Toggle Plain Text)
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Controls.ClearControls() 'To clear Textboxes only 'Me.Controls.ClearControls(Of TextBox)() End Sub End Class Public Module Extension Private Sub ClearTextBox(ByVal T As TextBox) T.Clear() End Sub Private Sub ClearCheckBox(ByVal T As CheckBox) T.Checked = False End Sub Private Sub ClearListBox(ByVal T As ListBox) T.Items.Clear() End Sub <Runtime.CompilerServices.Extension()> _ Public Sub ClearControls(ByVal Controls As Control.ControlCollection) For Each Control In Controls If ControlDefaults.ContainsKey(Control.GetType()) Then ControlDefaults(Control.GetType()).Invoke(Control) End If Next End Sub <Runtime.CompilerServices.Extension()> _ Public Sub ClearControls(Of T)(ByVal Controls As Control.ControlCollection) If Not ControlDefaults.ContainsKey(GetType(T)) Then Return For Each Control In Controls If Control.GetType().Equals(GetType(T)) Then ControlDefaults(GetType(T)).Invoke(Control) End If Next End Sub Private _ControlDefaults As Dictionary(Of Type, Action(Of Control)) Private ReadOnly Property ControlDefaults() As Dictionary(Of Type, Action(Of Control)) Get If (_ControlDefaults Is Nothing) Then _ControlDefaults = New Dictionary(Of Type, Action(Of Control)) _ControlDefaults.Add(GetType(TextBox), AddressOf ClearTextBox) _ControlDefaults.Add(GetType(CheckBox), AddressOf ClearCheckBox) _ControlDefaults.Add(GetType(ListBox), AddressOf ClearListBox) End If Return _ControlDefaults End Get End Property End Module
Last edited by adatapost; 20 Days Ago at 10:13 pm.
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- Receive Email in vb.net 2005 Part 2 (VB.NET)
- Help needed with code to add record to access database using VB 2005 (Visual Basic 4 / 5 / 6)
- GroupBox selection and Winform Clearing (C#)
- clear ALL textboxes (VB.NET)
- create textbox in code and set it equal to one on form. (C#)
- clear form data (ASP)
- Ref. 20 textboxes on a form numerically (VB.NET)
Other Threads in the VB.NET Forum
- Previous Thread: Serial State Change Monitoring
- Next Thread: Tab Control in VB.Net
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account arithmetic array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google gridview hardcopy html images input insert intel internet mobile monitor net networking objects open output panel passingparameters pdf picturebox port position print printing problem save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year






