Connecting a button and a text box in Visual Basic
-
05-09-2019 - |
Question
I'm stuck on this exercise that asks me to create a text box and a button. Each time the button is pressed it is supposed to add 1 to the text box. Anyone know how to approach this?
Solution
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i as Integer = int.Parse(TextBox1.Text)
i += 1
TextBox1.Text = i.ToString()
End Sub
You will also want to protect any attempt to key press in the textbox or set it where Enabled=true.
OTHER TIPS
- Add an event handler to the button
- When the event is triggered:
- Read the text value in the text box
- Convert it to a number
- Increase the value of the number by one
- Put that number back to the textbox
- PROFIT :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow