An Enhanced TextBox Control

The c text Box control is a custom ActiveX control that enhances the operation of the standard TextBox control. It’s a simple control (it inherits all of the TextBox control’s  functionality) but I found it very useful in designing data-entry applications. As you will see the enhancements are quite simple and really trivial to implement.

 Most of you have developed data-entry screens in which some fields are required and you may have designed Forms with TextBoxes colored differently depending on whether the corresponding field is required. Or you may have used the required field’s Lostfocus event to keep the focus in the required field until the user enters a value.

How about an enhanced TextBox control that changes color after a value is entered? Figure 16.18shows a data-entry Form using the enhanced TextBox control. The fields First Name Last Name and E-Mail are required and the corresponding TextBox controls are initially colored red. If the user moves the focus without entering a value in these fields they remain red. If a value is entered their color changes to white. Another feature of the enhanced TextBox control is.that it changes color when it receives the focus so that the user can quickly spot the active control on the Form. As you can probably guess I got the idea from data-entry Forms on Web pages which use an asterisk to indicate the required fields.

The CTextBoxcontrol
enhances the standard
TextBox control by adding a
few custom properties.

FIGURE 16.18:
FIGURE 16.18:

VB6 at Work The C TextBox Project

Open the c text Box project on the CD and run it. When you open the project for the first time you’ll se an error message indicating that the c text Box control can’t be loaded. Continue loading the project and then open the project’s test

Form. All instances of the C Text Box control are replaced by Picture Boxes. Delete the Picture Box controls’ on the Form and create an array of seven C Text Box controls. Place them on the Form as shown in Figure 16.18 aligning them with their corresponding captions and with one another. Then run the project by pressing F5 and check out the functionality of the new control.

The CTextBox Controls Specifications

The design of the C Text Box control is fairly simple. It’s identical to the standard Text Box control and it provides a few additional properties.

EnterFocusColor

When the control receives the focus its background color is set to this value. If you don’t want the currently active control to change color set its Enter Focus Color to white.

Leave Focus Color

When-the control loses the focus, its background color is set to this value (this property is usually white for optional fields and has the same value as the Mandatory Color for required fields).

Mandatory

This property indicates whether the control corresponds to a required field if Mandatory = 1 (Required) or an optional field if Mandatory = 0 (Optional).

MandatoryColor

This is the background color of the control if its Mandatory property is 1 (Required). The MandatoryColor overwrites the LeaveFocusColor setting. In other words, if the user skips a mandatory field, the corresponding control is painted with the MandatoryColor and not with the LeaveFocusColorNotice that required fields (those with Mandatorye l) behave like optional fields
after they have been assigned a value. .

To understand how these properties are used in the design of a data-entry Form open the CTextBox project and experiment with the settings of its custom properties to see how they affect its operation. Because the CTextBox control is not a standard element of the Windows interface your users may not understand what the .changing colors mean but it won’t take long for anyone to get the hang of than use this feature efficiently.

Designing the CTextBox Control

The design of the CTextBox control is straightforward. We’ll use the ActiveX Control Interface Wizard to design a custom control that has all the members of the standard TextBox control (except for the properties that relate to data binding). The Wizard will create the source code for us, and we’ll add a few statements that change the control’s.background color according to the settings of its properties
and its contents

Start a new ActiveX control project and add a test project as usual. then name the project’s components as follows:
1. Select the project and change its name to Color Text Box.
2. Select the UserControl object and change its name to C Text Box.
3. Select the test project and change its name to Test Project.
4. ‘Select the test Form and change its name to Test from

Since the custom control is nothing less than a Text Box control place an instance of the Text Box control on it. The TextBox control must cover entire User Control object so you must enter the following code in the user Control object’s Re size event handler:

Private Sub User Control_Resize()
Text1.Move 0 0 User Control.Width User Control.Height
End Sub

The remaining custom code must use the Mandatory property, so we can’t add it now. At this point you can start the ActiveX Control interface Wizard to generate most of the code. Our goal is to incorporate all the functionality of the Text Box control into our custom control.

In the Select Interface Members window, move the following members from the Available Names list to the Selected Names list:

These are the basic members of the Text Box control with the exception of the data-binding properties (Data Source Data Member and so on). When you duplicate the functionality of an existing control With your custom control you must make sure that all the members a developer expects to find in the custom control are there. I have skipped the data-binding properties because I don’t plan to use – this control with databases. (The topic of creating data-bound controls is fairly advanced and is not covered in this book.) .

Click the Next button to display the next window, Create Custom Interface Members. Here you must add the following custom members

Enter Focus Color The control’s background color when it receives the focus.

Leave focus Color The control’s background color when it loses the focus.

Mandatory If this property is True the control’s background is set to the Mandatory Color property’s value to indicate that the control is used for a  required field.

Mandatory Color The control’s background color when its Mandatory property is True.

In the next window of the Wizard map all members except the custom member to the corresponding properties of the text Box 1 control. No members of the C Text  Box control are mapped to the User Control object simply because the Text Box takes over the entire User Control object.

In the next window Set Attributes you define the properties of the custom properties.Enter the attributes shown in Table 16.4 in the appropriate fields in the Set Attributes window of the Wizard.

Properties of the Custom Properties
Properties of the Custom Properties

In the same window, you also enter a description for each property, which will appear in the Properties window when the corresponding property is selected. Click the Next button and then click Finish to generate the control’s code.

You can now open the User Control object’s Code window and see the code generated by the Wizard. The following variables and initial values appear at the top of the Code window:

‘Default Property Values:
Const m_def_Mandatory – False
Const m_def_EnterFocusColor – &HFFOOFF
Const m_def_MandatoryColor – &HFF
Const m_def_LeaveFocusColor – &HFFFFFF
‘Property Variables:
Dim m_Mandatory As Boolean
Dim m_EnterFocuscolor As Variant
Dim m_MandatoryColor As OLE_COLOR
Dim m_LeaveFocusColor As OLE_COLOR

These variable and constant definitions correspond to the properties we specified in the windows of the Wizard. All standard members have already been important  for you and you need not change them except for the Property procedures  of the Appearance property. The Wizard will implement this property as an Integer  but an Enumerated type works better for properties with a limited number of settings. So insert the following Type declaration:

Enum Flat3D
Flat
[3D]
End Enum
and then change the Property procedures as follows.

The Revised Appearance Property Procedures

Public Property Get Appearance() As Flat3D
Appearance = Text1 Appearance
End Property
Public Property Let Appearance(ByVal New_Appearance As Flat3D)
Textl.Appearance() = New_Appearance
PropertyChanged ‘Appearance’
End Property

You must also modify the code of the Property Let procedure for the Mandatory Color property. This property can be set only if the control’s Mandatory property i True. If it’s False the user must first change it and then set the Mandatory Color property.

The Revised Mandatory Color Property Procedures

Public Property Let Mandatory Color(By Val New_Mandatory Color As
OLE_COLOR)
m_MandatoryColor ~ New_MandatoryColor
If m_Mandatory Then Textl.BackColor = New_MandatoryColor
PropertyChanged ‘MandatoryCo1.or’
End Property

The Mandatory property is an integer and you can. enter any integer values in its field in the Properties window. Define the following Enumerated type:

Enum ReqOpt
[Optional]
R~quired
End Enum

Notice that the Optional value must be enclosed in square brackets, because it’s a Visual Basic keyword. Modify the definitions of the Property procedures of the Mandatory property as follows

The Modified Mandatory Property Procedures

Public Property 6et Mandatory() As ReqOpt
Mandatory am_Mandatory
End Property

Public Property Let Mandatory(ByVal ~ew_Mandatory As ReqOpt)
m_Mandatory = New_Mandatory .
If m_Mandatory – Required Then
Textl.BackColor = m_MandatoryColor
Else
Textl.BackColor = m_def_LeaveFocusColor
End If
PropertyChanged ‘Mandatory”
End Property

When the Mandatory property is set to True, the control automatically sets its background color property to the color value of the Mandatory Value property. This takes care of the trivial code necessary for the control’s proper operation. You’re now ready to add the custom code to enhance the operation of the Text Box control. The code that enhances the operation of the Text Box control is located in the User Control object’s Lost Focus event. When the user moves the focus away from a C Tcxt Box control the code examines the control’s contents and its Mandatory property. If the control is empty and its Mandatory property is True the Text- Box control’s background is set to the value of the Mandatory Color property.

Scroll to Top