Freetutes.com - Free Visual basic Tutorial website on the web

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Sponsored Links

Using TextBox Control In Visual Basic

A TextBox control is an edit field or edit control, displays information entered at design time, entered by the user, or assigned to the control using code at run time.

Setting properties to a TextBox

 
* Text can be entered into the text box by assigning the necessary string to the text property of the control

* If the user needs to display multiple lines of text in a TextBox, set the MultiLine property to True

* To customize the scroll bar combination on a TextBox, set the ScrollBars property.

* Scroll bars will always appear on the TextBox when it's MultiLine property is set to True and its ScrollBars property is set to anything except None(0)

* If you set the MultilIne property to True, you can set the alignment using the Alignment property. The test is left-justified by default. If the MultiLine property is et to False, then setting the Alignment property has no effect.

* In order to work with the part of a text in a text box, the text can be selected using three properties:

  • SelLength - Returns or sets the number of characters selected.

  • SelStart - Returns or sets the starting point of selected text. When no text is selected, SelStart indicates the position of the inserted point.

  • SelText - Returns or sets the string containing the currently selected text. If no text is selected, SelText consists of a zero-length string.

    The selected text can be copied to the Clipboard by using SelText:

    Clipboard.SelText text, [format]

    In the above syntax, text is the text that has to be placed into the Clipboard, and format has three possible values.

    1. VbCFLink - conversation information
    2. VbCFRTF - Rich Text Format
    3. VbCFText - Text

We get text from the clipboard using the GetText() function this way:

Clipboard.GetText ([format])

The following Figure summarizes the common TextBox control's properties and methods.

Property/ Method
Description

 

Properties

Enabled specifies whether user can interact with this control or not
Index Specifies the control array index
Locked If this control is set to True user can use it else if this control is set to false the control cannot be used
MaxLength Specifies the maximum number of characters to be input. Default value is set to 0 that means user can input any number of characters
MousePointer Using this we can set the shape of the mouse pointer when over a TextBox
Multiline By setting this property to True user can have more than one line in the TextBox
PasswordChar This is to specify mask character to be displayed in the TextBox

ScroolBars

 

This to set either the vertical scrollbars or horizontal scrollbars to make appear in the TextBox. User can also set it to both vertical and horizontal. This property is used with the Multiline property.
Text Specifies the text to be displayed in the TextBox at runtime
ToolTipIndex This is used to display what text is displayed or in the control
Visible By setting this user can make the Textbox control visible or invisible at runtime

 

Method

 
SetFocus Transfers focus to the TextBox

 

Event procedures

 
Change Action happens when the TextBox changes
Click Action happens when the TextBox is clicked
GotFocus Action happens when the TextBox receives the active focus
LostFocus Action happens when the TextBox loses it focus
KeyDown Called when a key is pressed while the TextBox has the focus
KeyUp Called when a key is released while the TextBox has the focus

Using a Label

Its a graphical control user can use to display uneditable text.

Properties of a Label Control

* We can write code that changes the caption property displayed by a Label control in response to events at run time. We can also use a label to identify a control, such as TextBox control, That doesn't have its own Caption property.

* The Autosize and WordWrap properties should be set if the user wants the Label properly display variable-length lines varying numbers of lines.

Using a CommandButton

We can begin, interrupt or end a process using the CommandButton control

Properties of a CommandButton control

* To display text on a CommandButton control, set its caption property.

* An event can be activated by clicking on the CommandButton.

* To set the background colour of the CommandButton, select a colour in the BackColor property.

* To set the text colour set the Forecolor property.

* Font for the CommandButton control can be selected using the Font property.

* To enable or disable the buttons set the Enabled property to True or False

* To make visible or invisible the buttons at run time, set the Visible property to True or False.

* Tooltips can be added to a button by setting a text to the Tooltip property of the CommandButton.

* A button click event is handled whenever a command button is clicked. To add a click event handler, double click the button at design time, which adds a subroutine like the one given below.

Private Sub Command1_Click( )
..................
End Sub

Using OptionButton Control

OptionButon provides a set of choices from which a user can select only one button by

  • Clicking it at run time

  • Assigning the value of the OptionButton in to True. The code is to assign it to True Option1.Value = True

  • Using the shotcut keys specified in the Caption of a Label

You can disable or enable the option button by setting the Enabled property to True os False. You can use the Visible property to make the option button visible to invisible.

The following example contains a Label, TextBox, CommandButton and three OptionButton controls.

Example

Open a new Standard EXE project and the save the Form as Option.frm and save the project as Option.vbp

Object
Property
Settings
Label

Caption

Name

Amount

lblAmount

TextBox

Text

Name

(empty)

txtAmount

CommandButton

Caption

Name

Exit

cmdExit

OptionButton

Caption

Name

Without meal

optWithoutMeal

OptionButton

Caption

Name

With Meal

optWithMeal

Option Button

Caption

Name

Luxury Room

optLuxury

Form

Caption

Name

Select Room Type

frmType

The application responds to the following events

The click event of the optWithoutMeal displays the amount of 2500 in txtAmount.

The click event of the optWithMeal displays the amount of 3500 in txtAmount.

The click event of the optLuxuty displays the amount of 5000 in txtAmount.

The click event of the cmdExit terminates the program

Following code is typed in the click events of the option buttons and the Exit button.


The Application is run by pressing F5 or clicking on the Run icon in the tool bar. By pressing the Exit button the program is terminated.

( Download the Source file)

Previous Page | Table of Contents | Next Page

 

 

 

   
Home | Link to Us | Partner Links | About Us | Contact Us
Copyright © 2007-2008 Freetutes.com | All Rights Reserved