Textboxes for input

To this point, our programs have executed without any input from the user. In this example, we see that Textboxes can be used for input from the user as well as for output. This seems like a simple addition, but if the computer can detect what the user types in, we can build interactive programs that respond to the user.

When a user types something into a Textbox, it is not just displayed on the screen, it is also assigned to the Text property of the Textbox. This is illustrated in our first Greeting program.

Whatever the user types into the input Textbox (txtNameIn) is displayed there and also assigned to its Text property.

The event handler for clicking on the "Greet" button uses two assignment statements:

Private Sub btnGreet_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnGreet.Click
   lblGreeting.Text = "Hi " & txtNameIn.Text & "!"
   txtGreeting.Text = "Hi " & txtNameIn.Text & "!"
End Sub
The first instruction tells the computer to evaluate the string expression "Hi " & txtNameIn.Text & "!" and assign the result to the Text property of lblGreeting. The second assigns the value of the expression to the Text property of txtGreeting.

Note that the user cannot type in the Textbox that is intended for output (txtGreeting) because I set its ReadOnly property to true.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.