Results 1 to 10 of 10

Thread: Best way to append data into a textbox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Best way to append data into a textbox?

    I want to write a coin flip simulator.
    I want to do a loop and print the flips.

    Should I use a textbox, or is there a better way?

    Also, do I append the flip to the existing output?
    Ideal way?

    loop
    Code:
    generate flip
    str = str & flip 
    textbox.text = str

    Is there a better way?/

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Best way to append data into a textbox?

    I would suggest using a ListBox since it is designed to display simple collections.

    Take a look at this example with a ListBox and a Button:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Add(FlipCoin())
    End Sub
    
    Private Function FlipCoin() As String
        Return If(DateTime.Now.Ticks Mod 2 = 0, "Heads", "Tails")
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Re: Best way to append data into a textbox?

    Listbox.add() is way cleaner
    Let me play with that.
    But what about being able to display new flips across a line of text (like in a textbox)
    vss. one per line

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Best way to append data into a textbox?

    You can use a TextBox, just keep in mind that Strings are immutable. Once String is created its data can't be changed, rather what happens is an entirely new String is created when you concatenate a value. This doesn't matter so much for a little bit of text, but theoretically you could see a performance hit once the size of the String increases.

    As far as how to do it, you would simply concatenate the value to the end of the Text property:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text &= " " & FlipCoin()
    End Sub
    
    Private Function FlipCoin() As String
        Return If(DateTime.Now.Ticks Mod 2 = 0, "Heads", "Tails")
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Re: Best way to append data into a textbox?

    Thanks for the ideas!

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Best way to append data into a textbox?

    FYI - There is also the AppendText textbox function. Me.TextBox1.AppendText(" abc")

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Best way to append data into a textbox?

    Quote Originally Posted by dday9 View Post
    You can use a TextBox, just keep in mind that Strings are immutable. Once String is created its data can't be changed, rather what happens is an entirely new String is created when you concatenate a value.
    I fail to see what you mean here
    if I do this
    Code:
    dim text as string
    text= "test 1"
    text= "test 2"
    the value of text will change in the same way than that
    Code:
    dim text as integer
    text= 1
    text= 2
    or do you mean there is something more behind text= "test 2" something like "text = New String(CType("test 2", Char()))" because a string is a collection of char? so it doesn't change the value but create a new object?
    Last edited by Delaney; Jan 15th, 2021 at 03:29 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    Re: Best way to append data into a textbox?

    Quote Originally Posted by wes4dbt View Post
    FYI - There is also the AppendText textbox function. Me.TextBox1.AppendText(" abc")
    Nice

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Best way to append data into a textbox?

    Quote Originally Posted by Delaney View Post
    I fail to see what you mean here
    if I do this
    Code:
    dim text as string
    text= "test 1"
    text= "test 2"
    the value of text will change in the same way than that
    Code:
    dim text as integer
    text= 1
    text= 2
    or do you mean there is something more behind text= "test 2" something like "text = New String(CType("test 2", Char()))" because a string is a collection of char? so it doesn't change the value but create a new object?
    What dday9 means is that you can't change a String OBJECT once it is created. That doesn't mean that you can't change a String VARIABLE. If you do this:
    vb.net Code:
    1. Dim str = "Hello"
    2.  
    3. str &= " World"
    some people think that the second substring is somehow incorporated into the String object that str already refers to but it is not. What actually happens is that a new String object is created that is the result of the concatenation and that new object is assigned to the variable. The String object that contained "Hello" and was originally assigned to the variable still exists in memory but is inaccessible and will eventually be cleaned up by the GC.
    Last edited by jmcilhinney; Jan 16th, 2021 at 09:03 AM. Reason: Replaced "create" with "change".

  10. #10
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Best way to append data into a textbox?

    Ok. Thank you for the explanation.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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