| | |
code to clear all textboxes in a form
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 4
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; Nov 7th, 2009 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 Nov 4th, 2009
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; Nov 7th, 2009 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: 321
Reputation:
Solved Threads: 45
0
#4 Nov 6th, 2009
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; Nov 6th, 2009 at 9:30 am.
0
#5 Nov 6th, 2009
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; Nov 6th, 2009 at 10:13 pm.
![]() |
Similar Threads
- clear ALL textboxes (VB.NET)
- 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#)
- 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: Convert 1-Dim String Array to String
Views: 821 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 2008 access add application array assignment basic box button buttons center class click code combo convert cpu data database datagrid datagridview design designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees error excel exists firewall function image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net opacity page pan picturebox port print printing printpreview problem record refresh regex reports" reuse right-to-left save savedialog search serial socket sorting sql sqldatbase storedprocedure string structures studio temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web wpf xml






