Saturday, January 9, 2010

TextBox control Tutorial

Textbox control for user input

TextBox is the widely used windows control in visual basic. No software application is possible without a text box. All string and numeric data is received via text box control. Whether it is a customer name, supplier name, age, amount, address or any other user data is retrieved through text box. Textbox is for basic text input without formatting. For formatted text (Font style, bold /italic, alignment options) we use RichTextBox control, an Activex control in VB 6.0. In .NET Richtextbox is an intrinsic control.



This tutorial will help you understand the Visual Basic TextBox control and its important properties, methods and events.

Name property: A unique name given to the textbox control on a windows Form. Usually 'txt' is the prefix used for textbox. For example, if you use a textbox to get customer's name, txtCustomer is the naming convention.

Text property: Used to Get or Set the text in the text box control.
To retrieve the text use variable = txtCustomer.Text, to set the text use txtCustomer.Text = 'Peter'

BackColor and ForeColor are used to set or Get the background and foreground color of the textbox control, respectively.

If you want the textbox control to bound to a data source, you can set the DataSource property of the textbox. You can set ODBC, OLEDB data source for the textbox. Set the DataMember property to the field in the database table. For example if you want to set the Customer tables, customer name property you can do it with datamember property. Use recordset's MoveNext, MovePrevious methods to navigate through record set.

PasswordChar property: PasswordChar is used for textbox for password input. The input character is replaced by password character defined by PasswordChar property. Usualy the asterisk (*) is used for password character. For security purpose the input character will be replaced by password character.

MaxLength Property: To define the maximum acceptable length of text in the textbox. For example you can set 20, to customer name textbox to restrict the number character in customer name to 20.

Locked Property: If set to True you can not edit the text in the textbox control.

Enabled Property: If set to false you can set focus on the control. Otherwise the textbox control will not receive focus and will not respond to user generated events.

Font Property: To set the Font family, font size and font style this property is used.

Multiline property: By default textbox control contain only one line. The text can not be wrapped to next line. To allow the textbox to receive carriage returns set the multiline property to true.

Visible Property: If set to false the textbox will be hidden during application run time. You can set true to show the control.

Methods: The SetFocus and Refresh are the few important events supported by the TextBox control.

TextBox events:

Change event: This event occurs when the text inside the textbox control changes. This is usually the keyboard keypress, keydown event.

GotFocus event: This event occurs when the textbox control Receives focus.

KeyDown event: Keydown occurs when the user press a key including control keys. KeyCode and Shift are the arguments in keydown event. The keycode contains the ASCII code of the key pressed. For example, if 'A' is pressed the keycode will contain 65, the ASCII equivalent for 'A'. The shift argument tells the state of the shift key. '0' means shift key not pressed. '1' means shift key was pressed.

KeyPress event: When a printable character is pressed the keypress event will fire. The only argument KeyAscii contains the ASCII code of the key pressed. To get the appropriate character us Chr(keyAscii) method. The non printable controls include, the CAPS key, Insert key, Delete Key, Home key, End key, Page up key, Page down key. These keys are not captured in keypress event. To capture these keys use the KeyDown event.

MouseDown, MouseMove, MouseUp events are fired when mouse button goes down, up.

Validate event: This event is fired when the textbox control is about to lose focus. The cancel property can be set to True to keep the focus. In other words you can validate the contents in the textbox and warn the user if the input is in wrong format or not in accordance with the business logic. For example you can validate it is valid number or not using IsNumberic function. If not numeric set Cancel to True and warn user with a message box pop up.

I think i have covered the important Properties, methods and events of the textbox control. I will write separate articles for specific usage of textbox control.