Results 1 to 9 of 9

Thread: Transferring strings from a List to a TextBox

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2011
    Posts
    32

    Transferring strings from a List to a TextBox

    Code:
    Dim Code_List As New List(Of String)
    Dim Code as String
    
    'load List with 1 of each Code
                    If Not Code_List.Contains(Code) Then
                        Code_List.Add(Code & vbTab)
                    End If
    
    TextBox1.Text = Code_List.???
    Can someone pls advise me how to transfer all the Codes (strings), which are accumulated in the List, to a TextBox?

    Thanks.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: Transferring strings from a List to a TextBox

    Well, given the code you've shown above, the list will only have one string in it, so you can simply assign TextBox1.Text = Code.

    But now assuming you have more than one item in the list, you could try something like this:
    vb Code:
    1. TextBox1.Text = Join(Code_List.ToArray, ", ")
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,684

    Re: Transferring strings from a List to a TextBox

    You have a few options:

    String.Join:
    Code:
    Dim Code_List As List(Of String) = New List(Of String) From {"one", "two", "three", "four", "five"}
    Dim Code As String = String.Join(", ", Code_List.ToArray())
    'TextBox1.Text = Code
    Console.WriteLine(Code)
    StringBuilder:
    Code:
    Dim Code_List As List(Of String) = New List(Of String) From {"one", "two", "three", "four", "five"}
    Dim Code As StringBuilder = New StringBuilder()
    Code_List.ForEach(Function (s) Code.Append(s & ", "))
    'TextBox1.Text = Code.ToString()
    Console.WriteLine(Code.ToString())
    LINQ:
    Code:
    Dim Code_List As List(Of String) = New List(Of String) From {"one", "two", "three", "four", "five"}
    Dim Code As String = Code_List.Aggregate(Function(c, n) c + ", " + n)
    'TextBox1.Text = Code.ToString()
    Console.WriteLine(Code.ToString())

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2011
    Posts
    32

    Re: Transferring strings from a List to a TextBox

    I see now that the List has only one string in it, the last Code out of many Codes. The List is in a For-To-Next loop. How to load all the Codes into the List?

  5. #5
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: Transferring strings from a List to a TextBox

    Show us the For..Next loop that has all the codes in it.
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,407

    Re: Transferring strings from a List to a TextBox

    Code:
    Dim Code_List As New List(Of String)
    Dim Code as String
    
    'load List with 1 of each Code
    If Not Code_List.Contains(Code) Then
        Code_List.Add(Code & vbTab)
    End If
    
    TextBox1.Lines= Code_List.ToArray

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2011
    Posts
    32

    Re: Transferring strings from a List to a TextBox

    Quote Originally Posted by dolot View Post
    Show us the For..Next loop that has all the codes in it.
    Code:
    For i = 1 To How_Many_DG_Cargos
                    DG_Position = Block_of_cntnr_info.IndexOf("IMD+", DG_Position + 3) 'find starting position of "IMD+"
                    If DG_Position <> -1 Then 'if IMD+ was found
                        Position_of_1st_PLUS = DG_Position + 3
                        Position_of_2nd_PLUS = Block_of_cntnr_info.IndexOf("+", Position_of_1st_PLUS + 1)
                        Length_of_Code = Position_of_2nd_PLUS - (Position_of_1st_PLUS + 1)
                        Code = Block_of_cntnr_info.Substring(Position_of_1st_PLUS + 1, Length_of_Code) 'extract Code
    
                    'load List with 1 of each Code
                    If Not Code_List.Contains(Code) Then
                        Code_List.Add(Code & vbTab)
                    End If
    
                Next i
    The List has only one string in it, the last Code out of many Codes. How to load all the Codes into the List?

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2011
    Posts
    32

    Re: Transferring strings from a List to a TextBox

    Quote Originally Posted by .paul. View Post
    Code:
    Dim Code_List As New List(Of String)
    Dim Code as String
    
    'load List with 1 of each Code
    If Not Code_List.Contains(Code) Then
        Code_List.Add(Code & vbTab)
    End If
    
    TextBox1.Lines= Code_List.ToArray
    The List is showing one string only. How to load all the Codes into the List?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,407

    Re: Transferring strings from a List to a TextBox

    where are you getting the codes from? we need to see that part of your code

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