VB.Net - TextBox Control

VB.Net provides several mechanisms for gathering input in a program. A TextBox control is used to display, or accept as input, a single line of text.

vb.net-textbox.jpg

VB.Net programmers make extensive use of the TextBox control to let the user view or enter large amount of text. A text box object is used to display text on a form or to get user input while a VB.Net program is running. In a text box, a user can type data or paste it into the control from the clipboard.

You can also collect the input value from a TextBox control to a variable like this way.

Dim var As String

var = TextBox1.Text

 

VB.Net TextBox Properties

You can set TextBox properties through Property window or through program. Normally Property window is located under the solution explorer. You can open Properties window by pressing F4 or right click on a control and select Properties menu item.

vb.net textbox properties

The below code set a textbox width as 150 and height as 25 through source code.

TextBox1.Width = 150

TextBox1.Height = 25

 

Background Color and Foreground Color

You can set Textbox background color and foreground color through property window, also you can set it programmatically.

TextBox1.BackColor = Color.Red

TextBox1.ForeColor = Color.Gray

 

TextBox BorderStyle

You can set 3 different types of border style for textbox in vb.net, they are None, FixedSingle and fixed3d.

TextBox1.BorderStyle = BorderStyle.FixedSingle

 

VB.Net TextBox Events

TextBox Keydown event

Keydown event occurs when a key is pressed while the control has focus.

e.g.

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

          If e.KeyCode = Keys.Enter Then

                   MessageBox.Show("Enter key pressed")

          ElseIf e.KeyCode = Keys.Escape Then

                   MessageBox.Show("Escape key pressed")

          End If

End Sub

 

TextChanged Event

TextChanged Event is raised if the Text property is changed by either through program modification or user input.

e.g.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,

                   ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Label1.Text = TextBox1.Text

End Sub

 

VB.Net Textbox Maximum Length

Maximum Length property sets the maximum number of characters or words the user can input into the text box control. That means you can limit the user input by this property.

TextBox1.MaxLength = 25

 

How to ReadOnly Textbox

when a program wants to prevent a user from changing the text that appears in a text box, the program can set the controls Readonly property is to True.

TextBox1.ReadOnly = True

 

Multiline TextBox in vb.net

By default TextBox accept single line of characters , If you need to enter more than one line in a TextBox control, you should change the Multiline property is to True.

TextBox1.Multiline = True

 

Textbox passowrd character

Sometimes you want a textbox to receive password from the user. In order to keep the password confidential, you can set the PasswordChar property of a textbox to a specific character.

TextBox1.PasswordChar = "*"

 

The above code set the PasswordChar to * , so when the user enter password then it display only * instead of typed characters.

How to Newline in TextBox

You can add new line in a textbox using two ways.

TextBox1.Text += "some text here" + "\r\n"

or

TextBox1.Text += "some text here" + Environment.NewLine

 

How to retrieve integer values from textbox ?

VB.Net String to Integer conversion

Dim i As Integer

i = Integer.Parse(TextBox1.Text)

Parse method Converts the string representation of a number to its integer equivalent.

VB.Net String to Double conversion

Dim dbl As Double

dbl = Double.Parse(TextBox1.Text)

 

How to allow only numbers in a textbox

Many of us have faced a situation where we want the user to enter a number in a TextBox. Click the following link that are going to make a Numeric Textbox which will accept only numeric values; if there are any values except numeric.

VB.Net Autocomplete TextBox

Autocomplete TextBox in VB.Net

From the latest version of Visual Studio, some of the controls support Autocomplete feature including the TextBox controls. The properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data source.

From the following VB.Net source code you can see some important property settings to a TextBox control.

Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Width = 200
    TextBox1.Height = 50
    TextBox1.Multiline = True
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim var As String
    var = TextBox1.Text
    MsgBox(var)
  End Sub

End Class
Next Post Previous Post
No Comment
Add Comment
comment url