VB.NET Technology

VB.Net Projects

VB.Net Project 1

The TextBox Control
Previous Home Next
adplus-dvertising

Text boxes should be used when you want the user to enter text that you have no knowledge of at design time (for example the name of the user). The primary function of a text box is for the user to enter text, but any characters can be entered, and it is quite possible to force the user to enter numeric values only.

Properties of TextBox

NameAvailabilityDescription
CausesValidationRead/WriteWhen a control that has this property set to true is about to receive focus, two events are fired: Validating and Validated.

You can handle these events in order to validate data in the control that is losing focus. This may cause the control never to receive focus. The related events are discussed below.

CharacterCasingRead/Write

A value indicating if the TextBox changes the case of the text entered. The possible values are

q Lower: All text entered into the text box is converted lower case.

q Normal: No changes are made to the text.

q Upper: All text entered into the text box is converted to upper case.

MaxLengthRead/WriteA value that specifies the maximum length in characters of any text, entered into the TextBox. Set this value to zero it the maximum limit is limited only by available memory.
MultilineRead/WriteIndicates if this is a Multiline control. A Multiline control is able to show multiple lines of text.
PasswordCharRead/WriteSpecifies if a password character should replace the actual characters entered into a single line textbox. If the Multiline property is true then this has no effect.
ReadOnlyRead/WriteA Boolean indicating if the text is read only.
ScrollBarsRead/WriteSpecifies if a multilane text box should display scrollbars.
SelectedTextRead/WriteThe text that is selected in the text box.
SelectionLengthRead/WriteThe number of characters selected in the text. If this value is set to be larger than the total number of characters in the text, it is reset by the control to be the total number of characters minus the value of SelectionStart.
SelectionStartRead/WriteThe start of the selected text in a text box
WordWrapRead/WriteSpecifies if a multiline text box should automatically wrap words if a line exceeds the width of the control.

Events of TextBox

Description
NameAvailability

Enter

GotFocus

Leave

Validating

Validated

LostFocus

These six events occur in the order they are listed here. They are known as "Focus Events" and are fired whenever a controls focus changes, with two exceptions. Validating and Validated are only fired if the control that receives focus has the CausesValidation property set to true.

The reason why it's the receiving control that fires the event is that there are times where you do not want to validate the control, even if focus changes. An example of this is if the user clicks a Help button.

KeyDown

KeyPress

KeyUp

These three events are known as "Key Events". They allow you to monitor and change what is entered into your controls.

KeyDown and KeyUp receive the key code corresponding to the key that was pressed. This allows you to determine if special keys such as Shift or Control and F1 were pressed.

KeyPress, on the otherhand, receives the character corresponding to a keyboard key. This means that the value for the letter "a" is not the same as the letter "A". It is useful if you want to exclude a range of characters, for example, only allowing numeric values to be entered.

ChangeOccurs whenever the text in the textbox is changed, no matter what the change.

TextBox Test

First Design a form on choosing New Project of Windows Application. and then set the properties of label, button and forms like shown in above picture.

Adding the events

Code for Ok Button Click

Private Sub ok_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles ok.Click
        If nametxt.Text = "" OrElse TextBox2.Text = "" OrElse TextBox3.Text = "" 
	OrElse TextBox4.Text = "" Then
            MessageBox.Show("Please fill all Information", "Alert", 
	MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
        Else
            Dim output As String
            'Concatenate the text values of the four TextBoxes
            output = "Name: " & Convert.ToString(Me.nametxt.Text) & vbCr & vbLf
            output += "Address: " & Convert.ToString(Me.TextBox2.Text) & vbCr & vbLf
            output += "Occupation: " & Convert.ToString(Me.TextBox3.Text) & vbCr & vbLf
            output += "Age: " & Convert.ToString(Me.TextBox4.Text)
            ' Insert the new text 
            Me.TextBox5.Text = output
        End If
    End Sub

Code for Help Button Click

Private Sub Button1_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        Dim output As String
        output = "Name =  Enter Your name" & vbCr & vbLf
        output += "Address = Enter Your address" & vbCr & vbLf
        output += "Occupation = Enter Occupation" & vbCr & vbLf
        output += "Age = Enter Your age"
        ' Insert the new text
        Me.TextBox5.Text = output
    End Sub

Adding Events on TextBox

Event on TextBox Leave Event

Private Sub nametxt_Leave(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles nametxt.Leave
        If nametxt.Text = "" Then
            nametxt.BackColor = Color.Red
        End If
    End Sub

Event on TextBox Keypress Event

Private Sub nametxt_KeyPress(ByVal sender As System.Object, 
	ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles nametxt.KeyPress
        If nametxt.BackColor = Color.Red Then
            nametxt.BackColor = Color.Snow
        End If
    End Sub
Previous Home Next