Results 1 to 8 of 8

Thread: Loading information into text boxes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Location
    Florida/Maryland
    Posts
    21

    Question Loading information into text boxes

    Is there a way to "automatically" load values into a multitude of text boxes in VB.Net?
    In version 6, we did this by using the text boxes’ index numbers in a loop.

    I am aware of the tabindex that is available in .Net, but if changes are made to the forms the controls might no longer be indexed contiguously.

    Please let me know if anyone has a suggestion.

    Terje

  2. #2
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    You could iterate through the forms control collection

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Location
    Florida/Maryland
    Posts
    21

    Question Collection

    Bananafish, thanks for your reply.

    Could you elaborate a little on your question?

    Thanks
    Terje

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    You can also use the ParamArray feature:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         ChangeTextBoxes("Hello", TextBox1, TextBox2, TextBox3)
    3.     End Sub
    4.  
    5.     Private Sub ChangeTextBoxes(ByVal sText As String, ByVal ParamArray textboxes() As TextBox)
    6.         Dim x As Integer
    7.         For x = 0 To textboxes.Length - 1
    8.             textboxes(x).Text = sText
    9.         Next
    10.     End Sub
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    this is the Control Collection way. Horses for courses I guess...

    VB Code:
    1. Dim oCtr As System.Windows.Forms.Control
    2.       For Each oCtr In Me.Controls
    3.          If oCtr.GetType.Name = "TextBox" Then
    4.             If oCtr.Name.StartsWith("txtAutoLoad") Then
    5.                'do whatever
    6.                txtAutoLoadA.Text = "aaa"
    7.                txtAutoLoadB.Text = "bbb"
    8.             End If
    9.          End If
    10.       Next

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Location
    Florida/Maryland
    Posts
    21
    Thanks for all your help.

    Terje

  7. #7
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    Actually - to be more dynamic the following code is better

    VB Code:
    1. Dim oCtr As System.Windows.Forms.Control
    2.       Dim oTxt As System.Windows.Forms.TextBox
    3.       For Each oCtr In Me.Controls
    4.          If oCtr.GetType.Name = "TextBox" Then
    5.             If oCtr.Name.StartsWith("txtAutoLoad") Then
    6.                'do whatever
    7.                oTxt = CType(oCtr, TextBox)
    8.                oTxt.Text = "aaa"
    9.             End If
    10.          End If
    11.       Next

  8. #8
    Ergo60
    Guest
    What kind of performance hit can this create? I have a form that has over 200 label controls, In VB6 it was easy using a control array, and an array to hold the values, and setting the value was as simple as using the same index for the control ans the underlying value in the array. In .Net I am forced into calling a routine that itterates through the controls collection.

    Is ther a way if I know the name of the control to find it, or set it quicker?, I dont really want to have to be stepping through the collection every time to find the correct control?

    Control arrays are the one thing I wish they had kept.

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