The New Buttoned Edit Control

A brand new control that extends the behavior of the Edit control is the ButtonedEdit component, which is a custom VCL control defined in the ExtCtrls unit. This is basically an edit box that can have small buttons on the left or right side, used to interact with the edit box itself. For example, you can add a Cancel button that empties the edit box, and a search or lookup button that validates the input or looks for some related information.

The Delphi IDE uses this component for the Search option of the Tools Palette, as you can see below:

Tool Palette

tei

1 ~ 1 fe 1 Search

E Standard

-

yUnwa^i^niBixi ® man™

u

m s> □

-

118 The internal code used to set up the text hint for a combo box in Windows XP and Windows Vista is different, but the VCL manages this for you. If you are interested in the details see the two sections of the method TCustomComboBox. DoSetTextHint for Windows version 5.1 (XP) and 6 (Vista).

This component, which requires XP or later versions of Windows, includes all of the new features of the Edit control, like the modern-looking text hint. Setting up the buttons on the sides of the edit box is quite simple. The component has a LeftButton and a RightButton property, of type TEditButton, defined as:

type

TEditButton published property property property property property property property end;

= class(TPersistent)

DisabledImageIndex: TImageIndex; DropDownMenu: TPopupMenu; Enabled: Boolean; HotImageIndex: TImageIndex; ImageIndex: TImageIndex; PressedImageIndex: TImageIndex; Visible: Boolean;

All of the image references are to the ImageList component you can hook to the ButtonedEdit control. You can attach a method to the click on either button using the OnLeftButtonClick and OnRightButtonClick events of the ButtonedEdit control; you can also attach a Popup menu to the buttons using the DropDownMenu property of the TEditButton class.

In the ButtonEdits demo I've coded some very simple usage scenarios, just to give an idea of how you can work with this component. At the same time showing some of the other new features introduced for edit boxes. The main form of the example sports three ButtonedEdit controls, two with a single button and one with two buttons. The controls also have text hints and one of them has a drop down menu attached. You can see the form at runtime (with the drop down menu active) in the following image:

Menu Drop Down Delphi

The first control is a numeric edit box with an undo button:

object edUndo: TButtonedEdit Images = ImageListl NumbersOnly = True RightButton.Imagelndex = 0 RightButton.Visible = True TextHint = 'A number'

OnRightButtonClick = edUndoRightButtonClick end

The edUndoRightButtonClick event handler calls the Undo method of the ButtonedEdit control. The second edit control provides two buttons, one for pasting from the clipboard and the second to clear the edit box content (thus restoring the text hint):

object edClear: TButtonedEdit Images = ImageListl LeftButton.Imagelndex = 3 LeftButton.Visible = True RightButton.Imagelndex = l RightButton.Visible = True TextHint = 'Some text'

OnLeftButtonClick = edClearLeftButtonClick OnRightButtonClick = edClearRightButtonClick end

The third edit box has a history button, and keeps track of the text that is entered in the window, allowing a user to reselect it:

object edHistory: TButtonedEdit Images = ImageListl

RightButton.DropDownMenu = PopupMenul RightButton.Imagelndex = 2 RightButton.Visible = True TextHint = 'Edit or pick' OnExit = edHistoryExit end

The component works by adding each new text to the popup menu as the user leaves the edit box, provided this text is not already in the menu:

procedure TFormButtonEdits.edHistoryExit(

Sender: TObject); begi n if (edHistory.Text <> '') and

(PopupMenul.Items.Find (edHistory.Text) = nil) then begi n

PopupMenul.Items.Add (Newltem (edHistory.Text, 0, False, True, RestoreText, 0, ''));

end;

262 - Chapter 8: VCL Improvements I end;

The predefined menu items and each new menu item added dynamically are connected with the RestoreText event handler which takes the caption of the selected menu items, strips any hot key, and copies it to the edit box:

procedure TFormButtonEdits.RestoreText(Sender: TObject); begi n edHistory.Text := StripHotkey ( (Sender as TMenuItem).Caption);

end;

Was this article helpful?

+2 -1
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment