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: little marine is an unknown quantity at this point 
Solved Threads: 0
little marine little marine is offline Offline
Newbie Poster

code to clear all textboxes in a form

 
0
  #1
23 Days Ago
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
  1. Private Sub btnclearall_Click
  2. Dim ctl As New Control
  3. For Each ctl In Me.Controls
  4. Next
  5. If Typeof ctl Is textbox Then
  6. Dim txtcontrol As Textbox = Directcast (ctl.Textbox)
  7. Me.text = StringEmpty
Thanks
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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: Rogachev is an unknown quantity at this point 
Solved Threads: 2
Rogachev Rogachev is offline Offline
Newbie Poster
 
0
  #2
23 Days Ago
Hello,

Try this

  1. For Each _control As Control In Me.Controls
  2. If TypeOf (_control) Is TextBox Then
  3. _control.Text = String.Empty
  4. End If
  5. 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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: michelle2025 is an unknown quantity at this point 
Solved Threads: 1
michelle2025 michelle2025 is offline Offline
Newbie Poster

Call ClearContent()

 
0
  #3
22 Days Ago
Hi there,

Please try this,

  1. Call ClearContent()

  1. Private Sub ClearContent()
  2. TxtBoxStatus.Text = ""
  3. End sub
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 288
Reputation: TomW is on a distinguished road 
Solved Threads: 37
TomW TomW is offline Offline
Posting Whiz in Training
 
0
  #4
20 Days Ago
  1. Private Sub btnclearall_Click
  2.  
  3. Dim ctl As New Control
  4.  
  5. For Each ctl In Me.Controls
  6. Next
  7.  
  8. 'Ok above you looped through all controls on your form but did
  9. 'nothing with each itteration
  10.  
  11. 'Now below is only checking one single control, the last one of your form
  12. If Typeof ctl Is textbox Then
  13.  
  14. 'Above you detemined ctl, is a texbox, so why are
  15. 'you creating a new text box and converting the
  16. 'the existing textbox into another textbox?
  17. Dim txtcontrol As Textbox = Directcast (ctl.Textbox)
  18.  
  19.  
  20. 'Me is the form you are in, so the only thing
  21. 'you are changing is the text propert of the form itself
  22. 'not any of the text boxes.
  23. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,605
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
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.

  1. Public Class Form1
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. Me.Controls.ClearControls()
  4.  
  5. 'To clear Textboxes only
  6. 'Me.Controls.ClearControls(Of TextBox)()
  7. End Sub
  8. End Class
  9. Public Module Extension
  10.  
  11. Private Sub ClearTextBox(ByVal T As TextBox)
  12. T.Clear()
  13. End Sub
  14.  
  15. Private Sub ClearCheckBox(ByVal T As CheckBox)
  16. T.Checked = False
  17. End Sub
  18.  
  19. Private Sub ClearListBox(ByVal T As ListBox)
  20. T.Items.Clear()
  21. End Sub
  22.  
  23. <Runtime.CompilerServices.Extension()> _
  24. Public Sub ClearControls(ByVal Controls As Control.ControlCollection)
  25. For Each Control In Controls
  26. If ControlDefaults.ContainsKey(Control.GetType()) Then
  27. ControlDefaults(Control.GetType()).Invoke(Control)
  28. End If
  29. Next
  30. End Sub
  31. <Runtime.CompilerServices.Extension()> _
  32. Public Sub ClearControls(Of T)(ByVal Controls As Control.ControlCollection)
  33. If Not ControlDefaults.ContainsKey(GetType(T)) Then Return
  34.  
  35. For Each Control In Controls
  36. If Control.GetType().Equals(GetType(T)) Then
  37. ControlDefaults(GetType(T)).Invoke(Control)
  38. End If
  39. Next
  40. End Sub
  41. Private _ControlDefaults As Dictionary(Of Type, Action(Of Control))
  42. Private ReadOnly Property ControlDefaults() As Dictionary(Of Type, Action(Of Control))
  43. Get
  44. If (_ControlDefaults Is Nothing) Then
  45. _ControlDefaults = New Dictionary(Of Type, Action(Of Control))
  46. _ControlDefaults.Add(GetType(TextBox), AddressOf ClearTextBox)
  47. _ControlDefaults.Add(GetType(CheckBox), AddressOf ClearCheckBox)
  48. _ControlDefaults.Add(GetType(ListBox), AddressOf ClearListBox)
  49. End If
  50. Return _ControlDefaults
  51. End Get
  52. End Property
  53. 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC