Results 1 to 6 of 6

Thread: Clearing multiple text fields

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    2

    Clearing multiple text fields

    I tried searching for a solution, but couldn't find anything, so if I am re-asking a common question, don't beat me too badly.

    For a class, I have to create a form that allows someone to enter in the names of six students, as well as five test scores each. It also averages the scores, and displays them in a label.

    What I am wondering is if there is a way to clear the text from all 36 text fields, as well as the 6 labels without having to do everything individually?
    I could certainly go through, and write out txtField1.text = String.Empty 36 times, but it seems there would be a more elegant solution.

    Also, if there is a way to do so, would the same would apply for putting the data entered into an array, or saving it to a file?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Clearing multiple text fields

    You can use a loop of some sort, but exactly how to do it depends on how your controls are laid out and/or named. For instance, you had TextBoxes named TextBox1 to TextBox36 then you could do this:
    vb.net Code:
    1. For n = 1 To 36
    2.     Me.Controls("TextBox" & n).ResetText()
    3. Next
    It's not really a good idea to use names like that though. Another option would be to have your controls in a TableLayoutPanel and then use nested loops to work through it column by column, row by row. As a last resort, you can just create your own array or collection and then loop through that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    2

    Re: Clearing multiple text fields

    I was just using txtField1 as an example.

    I will try out your suggestion, though, and see how it goes. Thanks!

  4. #4
    New Member srijithpv's Avatar
    Join Date
    Aug 2010
    Posts
    1

    Re: Clearing multiple text fields

    You can loop through the TextBox controls used in the form dynamically and set it to blank value

    vb Code:
    1. For Each myControls In Me.Controls
    2.             If TypeOf myControls Is TextBox Then
    3.                 myControls.Text = String.Empty
    4.             End If
    5.         Next

    same way, you can check the labels using "Label" instead of TextBox

    Hope this helps

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Clearing multiple text fields

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim textboxes() As TextBox = Me.Controls.OfType(Of TextBox).ToArray
    5.         textboxes = Array.ConvertAll(textboxes, AddressOf clearTextBox)
    6.     End Sub
    7.  
    8.     Private Function clearTextBox(ByVal tb As TextBox) As TextBox
    9.         tb.Text = ""
    10.         Return tb
    11.     End Function
    12.  
    13. End Class

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Clearing multiple text fields

    Array.ConvertAll isn't really appropriate, given that there's no conversion occurring. If this is .NET 4.0 then lambda expressions don't have to return a value:
    vb.net Code:
    1. Dim textBoxes = Me.Controls.OfType(Of TextBox)().ToArray()
    2.  
    3. Array.ForEach(textBoxes, Sub(tb) tb.Clear())
    If it's not .NET 4.0, or even if it is, you can just use a For Each loop:
    vb.net Code:
    1. For Each tb In Me.Controls.OfType(Of TextBox)()
    2.     tb.Clear()
    3. Next
    This assumes that you do want to clear every TextBox on the form of course.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width