r/visualbasic • u/halitoz3l5 • May 09 '23
I have very simple question that I couldn't make sense. Why do we have to put Textbox1.Text before "Hello"
If I don't add Textbox1.Text before "Hello"
It gives me one Hello
When added
It gives me 5 Hello's and my question is why do we have to put Textbox1.Text before "Hello" like I said I couldn't make sense of it.
5
Upvotes
8
u/grbc_ May 09 '23
Because you need to tell the computer where to put HELLO. You chose to put HELLO in the TextBox1 object, but you could easily imagine having 3 or 4 text boxes that need different information, for example, separate boxes for First Name, Last Name, and Greeting. If you don’t put TextBox1.Text before HELLO, then HELLO won’t show up anywhere.
2
u/Alpaca1061 May 10 '23 edited May 10 '23
It needs to know where and what. You have to set it equal to textbox1 so it knows where. You also need .text so it knows what data you're trying to change.
Also, you have a very simple program and a default element name and you seem new. So I'm going to give you tips for element names.
Letting X be what the element does, and using camel case (first word lowercase and the rest having and uppercase first letter and no spaces in between) here's hos you should name your variables
Text Box: txtX Button: btnX Radio Button: radX Check Box: chkX Form: frmX (this one you shouldn't change though)
This will help you better understand what's what. You should do the same for variable names, but you don't really need prefixes.
1
u/GlowingEagle May 09 '23 edited May 09 '23
For image 4, the content of the textbox is a string. You built the string by repeatedly adding HELLO (and a newline) to the previous content of the string, finally setting the textbox content to that string.
[edit] sorry, not quite right - try this: in each loop, you are adding HELLO to the content of the text box.
0
u/3583-bytes-free May 09 '23
A few suggestions:
"Dim n as integer" would be better just before the "For n .." as it has scope over the whole class where you have it which is bad practice.
Do you mean Textbox1.Text &= "HELLO"? As it stands you are just putting a single "HELLO" into the textbox repeatedly which is pointless.