VCL Controls: The Edit Control

 

Introduction

An Edit box is a Windows control used to get or display text for the userís interaction. At its most regular use, an Edit box serves as a place to fill out and provide information. Such a use is common on an employment application, login dialog boxes, etc.

Like most other controls, the role of an edit box is not obvious at first glance; that is why it should be accompanied by a label that defines its purpose. From the userís standpoint, an edit box is named after the label closest to it. Such a label is usually on the left or the top side of the edit box. From the programmerís point of view, an edit box is a place holder used for various things. For example, your can show or hide it as you see fit. You can also use it only to display a text without allowing the user to change it.

To create an edit box, once again, Bcb provides a lot of choices. The most fundamental edit box is designed with the Edit control of the Standard tab of the component palette.

Edit Box Properties

The most important edit box property for both the programmer and the user is the Text. When you add an Edit control, C++ Builder initializes it with the name of the control, this would be Edit1 for the first edit box, Edit2 for the second, etc. If you want the control to display some text when the form launches, type the text in the Text property field. Otherwise, if you want the edit box to be empty when it comes up for the first time, delete the content of the Text field.

Another important property of the Edit control is the Name. It makes it easy for you to identify the control and its events when writing code.

Although it does not belong to an Edit control, the FocusControl property of a Label control provides a convenient way to specify which control is associated with the label. For example, if a label has a caption of &Country and you want the user to press Alt + C to access the Edit control positioned under or on the right side of the label, select the name of such an Edit control in the FocusControl field of the Label control.

The CharCase property allows the content of an Edit control to apply a character case of your choice. The CharCase property is controlled by the TEditCharCase enumerator. By default, it is set to normal, which respects whatever character case is typed in an edit box. Otherwise, you can set it to ecUpperCase or ecLowerCase to set the edit boxí characters to uppercase or lowercase respectively.

When using a form, the user can press Tab to move from one control to another. By default, when such a control receives focus from the user pressing Tab, the whole text in an edit control is selected. This is controlled by the AutoSelect property. If you do not want that, you can set the AutoSelect Boolean property to false.

The Color property allows you to control the background color of an Edit control.

The Hint property provides a quick local help to the user. It is text that would appear if the user positions the mouse on a control for a few seconds. 

Edit Control Methods and Events

The Edit control has a constructor and a destructor used to dynamically create an edit control. The other methods are derived from the controlís direct parent which is the TCustomEdit class. Other methods are derived from the ancestor classes.

The Edit control is equipped with many events. Some of these events are from its parent class the TCustomEdit class and some others are from ancestor classes.

The OnEnter event occurs when a text-based control receives focus. This happens when the user clicks the edit box, after previously pressing Tab, to give focus to the control.

The OnChange event occurs as the user is typing text in the control. This happens as the user is changing the content of an edit control; this sends a message that the content of the edit box has changed or has been updated. You can use this event to check, live, what the user is doing in the edit box. For example, if you create a dialog with a first and last names edit boxes, you can use another edit box to display the full name. The controls could be drawn as follows:

You can implement the OnChange event of the first name edit box as follows:

//---------------------------------------------------------------------------
void __fastcall TfrmPersInfo::edtFirstNameChange(TObject *Sender)
{
    edtFullName->Text = edtFirstName->Text + " " + edtLastName->Text;
}
//---------------------------------------------------------------------------

When the second edit box is being edited, you can implement its OnChange event as follows:

//---------------------------------------------------------------------------
void __fastcall TfrmPersInfo::edtLastNameChange(TObject *Sender)
{
    edtFullName->Text = edtFirstName->Text + " " + edtLastName->Text;
}
//---------------------------------------------------------------------------

The OnExit event occurs when the control loses focus. In the case of an edit box, this could happen if the control has focus and the user presses Tab; the edit box would lose focus.

Keyboard Events: Another Review

Besides the Button control, probably the most interactive control on the form is the edit box; it can receive or display information, it can exhibit its own behavior. The events we will review here apply to all controls that have the keyboard events. Therefore, our demonstration will serve as a broad explanation of some events common to various controls.

When a control receives focus, the OnEnter event fires. When a control loses focus, the OnExit event fires. When the user presses a key, the OnKeyDown event fires. When the user releases a key, the OnKeyUp event fires.

When implementing the keyboard events, you can also perform a particular operation depending on the key that the user pressed. Each key has a virtual key code. You can find the list of these keys in the winuser.h header file. You can shift focus when the user presses Enter on an edit box using the following code:

//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
    if(Key == 13)
        Edit2->SetFocus();
}
//---------------------------------------------------------------------------

 

 
 

Home Copyright © 2004-2007 FunctionX, Inc.