-
Jan 15th, 2021, 11:26 AM
#1
Thread Starter
Hyperactive Member
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?/
-
Jan 15th, 2021, 11:42 AM
#2
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
-
Jan 15th, 2021, 12:14 PM
#3
Thread Starter
Hyperactive Member
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
-
Jan 15th, 2021, 12:20 PM
#4
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
-
Jan 15th, 2021, 01:58 PM
#5
Thread Starter
Hyperactive Member
Re: Best way to append data into a textbox?
-
Jan 15th, 2021, 03:05 PM
#6
Re: Best way to append data into a textbox?
FYI - There is also the AppendText textbox function. Me.TextBox1.AppendText(" abc")
-
Jan 15th, 2021, 03:22 PM
#7
Re: Best way to append data into a textbox?
Originally Posted by dday9
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)
-
Jan 15th, 2021, 05:22 PM
#8
Thread Starter
Hyperactive Member
Re: Best way to append data into a textbox?
Originally Posted by wes4dbt
FYI - There is also the AppendText textbox function. Me.TextBox1.AppendText(" abc")
Nice
-
Jan 15th, 2021, 09:00 PM
#9
Re: Best way to append data into a textbox?
Originally Posted by Delaney
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:
Dim str = "Hello"
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".
-
Jan 16th, 2021, 07:55 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|