|
TextBox Control
Windows users should be familiar with textboxes. This control looks like a box and
accepts input from the user. The TextBox is based on the TextBoxBase class
which is based on the Control class.
TextBoxes are used to accept input from the user or used to display text. By default
we can enter up to 2048 characters in a TextBox but if the Multiline property is set
to True we can enter up to 32KB of text. The image below displays a Textbox.
Some Notable Properties:
Some important properties in the Behavior section of the Properties Window for TextBoxes.
Enabled: Default value is True. To disable, set the
property to False.
Multiline: Setting this property to True makes the TextBox
multiline which allows to accept multiple lines of text. Default value is False.
PasswordChar: Used to set the password character. The text
displayed in the TextBox will be the character set by the user. Say, if you enter
*, the text that is entered in the TextBox is displayed as *.
ReadOnly: Makes this TextBox readonly. It doesn't allow
to enter any text.
Visible: Default value is True. To hide it set the property
to False.
Important properties in the Appearance section
TextAlign: Allows to align the text from three possible
options. The default value is left and you can set the alignment of text to right
or center.
Scrollbars: Allows to add a scrollbar to a Textbox. Very
useful when the TextBox is multiline. You have four options with this property. Options
are are None, Horizontal, Vertical and Both. Depending on the size of the TextBox
anyone of those can be used.
TextBox Event
The default event of the TextBox is the TextChanged Event which looks like this in
code:
Private Sub TextBox1_TextChanged(ByVal sender As
System.Object, ByVal e As _ System.EventArgs) Handles TextBox1.TextChanged
End Sub
|
Working With TextBoxes
Lets work with some examples to understand TextBoxes.
Drag two TextBoxes (TextBox1, TextBox2) and a Button (Button1) from the toolbox.
Code to Display some text in the TextBox
We want to display some text, say, "Welcome to TextBoxes", in TextBox1 when the Button
is clicked. The code looks like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Welcome to TextBoxes"
End Sub
|
Code to Work with PassWord Character
Set the PasswordChar property of TextBox2 to *. Setting that will make the text entered
in TextBox2 to be displayed as *. We want to display what is entered in TextBox2 in
TextBox1. The code for that looks like this:
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox2.Text
End Sub
|
When you run the program and enter some text in TextBox2, text will be displayed as
*. When you click the Button, the text you entered in TextBox2 will be displayed as
plain text in TextBox1.
Code to Validate User Input
We can make sure that a TextBox can accept only characters or numbers which can
restrict accidental operations. For example, adding two numbers of the form 27+2J
cannot return anything. To avoid such kind of operations we use the KeyPress
event of the TextBox.
Code that allows you to enter only double digits in a TextBox looks like this:
Private Sub TextBox1_KeyPress(ByVal sender As Object,ByVal e As_
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If(e.KeyChar < "10" Or e.KeyChar > "100") Then
MessageBox.Show("Enter Double Digits")
End If
End Sub
|
Creating a TextBox in Code
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As_
System.EventArgs)
Handles MyBase.Load
Dim TextBox1 as New TextBox()
TextBox1.Text="Hello Mate"
TextBox1.Location=New Point(100,50)
TextBox1.Size=New Size(75,23)
Me.Controls.Add(TextBox1)
End Sub
End Class
|
|