Navigation: TextEd > Extension and Scripts > Visual components >

TComboBox

 

 

 

A TComboBox component is an edit box with a scrollable drop-down list attached to it. Users can select an item from the list or type directly into the edit box.

 

In some examples we assume a check box has been created and is named "Combo1".

 

TComboBox Combo1 = TComboBox.Create(form);

 


Properties



 

property TAlign Align;

Determines how the control aligns within its container (parent control).


enum TAlign {

  alNone,

  alTop,

  alBottom,

  alLeft,

  alRight,

  alClient,

  alCustom

};

 

Ex. Combo1.Align = alNone;


 

property Bool AlignWithMargins;

Indicates whether a control should be constrained by margins. If AlignWithMargins is true, use the Margins property of the control to govern the spacing relative to other controls that are aligned with this one.


 

property TAnchors Anchors;

Specifies how the control is anchored to its parent.

 

enum TAnchorKind {

  akLeft,

  akTop,

  akRight,

  akBottom

};

 

Ex. Combo1.Anchors = akLeft + akTop;


 

property Bool AutoCloseUp;

Specifies whether the drop-down closes up automatically when the user selects an item.


 

property Bool AutoComplete;

Positions to matching list items as you type. As you type, the ItemIndex property is updated with the list entry that matches the characters you have entered. As you type, different list entries may match, and ItemIndex is changed accordingly.


 

property Int AutoCompleteDelay;

Specifies the delay between a key press and an attempt to autocomplete the field, given in milliseconds.


 

property Bool AutoDropDown;

Specifies whether the drop-down list drops down automatically in response to user keystrokes.


When AutoDropDown is true, the combo box automatically drops down its list when the user starts typing a string while the combo box has focus.


When AutoDropDown is false, the user must explicitly use the drop-down button to drop down the combo box list.


 

property TEditCharCase CharCase;

Determines the case of the text in the combo box.Use CharCase to force the contents of the combo box into upper or lower case.


TEditCharCase = (

  ecNormal,

  ecUpperCase,

  ecLowerCase

);

 

Ex. Combo1.CharCase = ecNormal;


 

property TColor Color;

Specifies the background color of the control. The color value can be an hexadecimal or a color constant.

 

Defined color constants are:

clAqua, clBlack, clBlue, clCream, clDkGray, clFuchsia, clGray, clGreen, clLime, clLtGray, clMaroon, clMedGray, clMoneyGreen, clNavy, clOlive, clPurple, clRed, clSilver, clSkyBlue, clTeal, clWhite, clYellow.


 

Defined system color constants:

clNone                     White on Windows 9x, Black on NT.

clScrollBar                Current color for the of scroll bar track.

clBackground               Current background color of the Windows desktop

clActiveCaption            Current color of the title bar of the active window

clInactiveCaption          Current color of the title bar of inactive windows

clMenu                     Current background color of menus

clWindow                   Current background color of windows

clWindowFrame              Current color of window frames

clMenuText                 Current color of text on menus

clWindowText               Current color of text in windows

clCaptionText              Current color of the text on the title bar of the active window

clActiveBorder             Current border color of the active window

clInactiveBorder           Current border color of inactive windows

clAppWorkSpace             Current color of the application workspace

clHighlight                Current background color of selected text

clHightlightText           Current color of selected text

clBtnFace                  Current color of a button face

clBtnShadow                Current color of a shadow cast by a button

clGrayText                 Current color of text that is dimmed

clBtnText                  Current color of text on a button

clInactiveCaptionText      Current color of the text on the title bar of an inactive window

clBtnHighlight             Current color of the highlighting on a button

cl3DDkShadow               Windows 95 or NT 4.0 only: Dark shadow for three-dimensional display elements

cl3DLight                  Windows 95 or NT 4.0 only: Light color for three-dimensional display elements (for edges facing the light source)

clInfoText                 Windows 95 or NT 4.0 only: Text color for tool tip controls

clInfoBk                   Windows 95 or NT 4.0 only: Background color for tool tip controls

clGradientActiveCaption    Windows 98 or Windows 2000: Right side color in the color gradient of an active window's title bar.

clActiveCaption            specifies the left side color.

clGradientInactiveCaption  Windows 98 or Windows 2000: Right side color in the color gradient of an inactive window's title bar. clInactiveCaption          specifies the left side color.

clDefault                  The default color for the control to which the color is assigned.


Ex. Combo1.Color = 0x0000FF00;

Ex. Combo1.Color = clGreen;


 

property Int DropDownCount;

Specifies the maximum number of items displayed in the drop-down list. By default, the drop-down list is long enough to contain eight items without requiring the user to scroll to see them all. To make the drop-down list smaller or larger, specify a number larger or smaller than eight as the DropDownCount value.


 

property Bool Enabled;

Controls whether the control responds to mouse, keyboard, and timer events.


 

property TFont Font;

Controls the attributes of text written on or in the control. To change to a new font, specify a new TFont object. To modify a font, change the value of the Charset, Color, Height, Name, Pitch, Size, or Style of the TFont object.

 

Ex. Combo1.Font.Size = 12;


 

property int Height;

Specifies the vertical size of the control in pixels.


Ex. Combo1.Height = 16;


 

property String Hint;

Contains the text string that can appear when the user moves the mouse over the control.


 

property Int ItemHeight;

Specifies the height, in pixels, of the items in the drop-down list.


 

property int ItemIndex;

Specifies the index of the selected item. Read ItemIndex to determine which item is selected. The first item in the list has index 0, the second item has index 1, and so on. If no item is selected, the value of ItemIndex is -1.


 

property TStrings Items;

Provides access to the list of items (strings) in the list portion of the combo box.

 

Ex. String s = Combo1.Items[0];

Ex. Combo1.Items[0] = "Hello World!";

 

Common TStrings methods:

 

       function    int Add(const String S);             // Adds a string at the end of the list.

       procedure   Clear();                             // Clear the entire list

       procedure   Delete(int Index);                   // Delete a string in the list

       function    int IndexOf(const String S);         // Returns the position of a string in the list.

       procedure   Insert(int Index, const String S);   // Insert string in a specific position

       procedure   LoadFromFile(const String FileName); // Load strings from a file

       procedure   SaveToFile(const String FileName);   // Save strings to a file


 

 

property int Left;

Specifies the horizontal coordinate of the left edge of a component relative to its parent.


 

property TMargins Margins;

Specifies the margins for the control. TMargins help define the relative position between components on a form, and between the edges of the form and the component. For example, when you set a left margin for a component to 10 pixels, the component will not come closer than 10 pixels to the edge of the container, or to another component on the left edge. The number of pixels by which two components are separated is the sum of the pixels of both components.

TMargins have the class members top, bottom, left and right.


Ex. Combo1.Margins.Left = 10;


 

property Int MaxLength;

Specifies the maximum number of characters the user can type into the edit portion of the combo box.


 

property String Name;

Specifies the name of the component as referenced in code.


 

property TPadding Padding;

Specifies the padding of a control. Padding works similar to margins.


Ex. Combo1.Padding.Left = 4;


 

property TWinControl Parent;

Use the Parent property to get or set the parent of this control. The parent of a control is the control that contains the control. For example, if an application includes three radio buttons in a group box, the group box is the parent of the three radio buttons, and the radio buttons are the child controls of the group box.

 

Ex.

TComboBox Combo1 = TComboBox.Create(form);

Combo1.Parent = form;

Combo1.Left = 8;


 

property Bool ParentColor;

To have a control use the same color as its parent control, set ParentColor to true. If ParentColor is false, the control uses its own Color property.


 

property Bool ParentFont;

Determines where a control looks for its font information.

To have a control use the same font as its parent control, set ParentFont to true. If ParentFont is false, the control uses its own Font property.


 

property TPopupMenu PopupMenu;

Identifies the pop-up menu associated with the control.


 

property int SelLength;

Specifies the length, in characters, of the selected text in the edit portion of the combo box. The selected text appears highlighted in the edit region of the combo box.

To select a section of text in the edit region, first set the SelStart property to position the cursor, and then set SelLength to the number of characters that should be selected. The SelText property is the substring that is selected.


 

property int SelStart;

Specifies the position of the first selected character in the edit portion of the combo box. Read SelStart to determine where the selection starts in the Text property. The first character is designated with 0, the second with 1, and so on. When SelLength is 0, SelStart is the position of the cursor.


 

property String SelText;

Represents the selected text in the edit region.

Read SelText to obtain the text that is selected in the edit region of the combo box.

Set SelText to replace the current selection with a different substring. If no text is selected, the SelText string is inserted in the text at the cursor.


 

property Bool ShowHint;

Determines whether the control displays a Help Hint when the mouse pointer rests momentarily on the control.


 

property Bool Sorted;

Determines whether the list portion of the combo box is sorted. Set Sorted to true to sort the items in the drop down list alphabetically. New items added to the list are inserted in the correct alphabetical position.


 

property TTabOrder TabOrder;

TabOrder is the order in which child windows are visited when the user presses the Tab key. The control with the TabOrder value of 0 is the control that has the focus when the form first appears.

 

Initially, the tab order is always the order in which the controls were added to the form. The first control added to the form has a TabOrder value of 0, the second is 1, the third is 2, and so on. Change this by changing the TabOrder property.  


 

property Bool TabStop;

Determines if the user can tab to a control.


 

property int Tag;

Stores an integer value as part of a component.


 

property String Text;

Use the Text property to read or set the Text of the edit control.

 

Ex. String s = Combo1.Text;


 

property int Top;

Specifies the Y coordinate of the top left corner of a control, relative to its parent or containing control in pixels.


 

property Boolean Visible;

Indicates whether the control is visible.


 

property int Width;

Specifies the horizontal size of the control or form in pixels.


 


Methods



 

constructor Create(TComponent AOwner) 

Creates and initializes a new TComboBox object.


Ex. TComboBox Combo1 = TComboBox.Create(form);


 

procedure Free;

Destroys an object and frees its associated memory, if necessary. Visual controls added to a form is automatically destroyed when the form closes.

 

Ex.

TForm form = new TForm(nil);

TComboBox Combo1 = TComboBox.Create(form);

...

Combo1.Free;


 

procedure Hide;

Hides the control.


 

procedure Show;

Shows the control. Use Show to set the control's Visible property to true.


 


Events



 

OnChange

Occurs when the user changes the text displayed in the edit region.


Ex.

 

TForm form;

TComboBox Combo1;

void Combo1Change(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);  

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnChange = &Combo1Change;

}


 

OnClick 

Occurs when the user clicks the control.


Ex.

 

TForm form;

TComboBox Combo1;

void Combo1Click(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);  

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnClick = &Combo1Click;

}


 

OnCloseUp 

Occurs when the drop-down list closes up due to some user action.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1CloseUp(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);  

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnCloseUp = &Combo1CloseUp;

}


 

OnDblClick 

Occurs when the user double-clicks the left mouse button when the mouse pointer is over the control.


Ex.

 

TForm form;

TComboBox Combo1;

void Combo1DblClick(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);  

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnDblClick = &Combo1DblClick;

}


 

OnDropDown 

Occurs when the user opens the drop-down list.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1DropDown(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);  

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnDropDown = &Combo1DropDown;

}


 

OnEnter 

Occurs when a control receives the input focus. Use the OnEnter event handler to cause any special processing to occur when a control becomes active.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1Enter(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnEnter = &Combo1Enter;

}

 

 

OnExit 

Occurs when the input focus shifts away from one control to another. Use the OnExit event handler to provide special processing when the control ceases to be active.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1Exit(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnExit = &Combo1Exit;

}


 

OnKeyDown 

Occurs when a user presses any key while the control has focus.


(ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble) TShiftState;

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1KeyDown(TObject Sender, WORD &Key, TShiftState Shift)

{

  // Do some work here

  if (Key == VK_F1) {

    // F1 was pressed, lets do something...

 

  }

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnKeyDown = &Combo1KeyDown;

}


 

OnKeyPress 

Occurs when a key is pressed.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1KeyPress(TObject Sender, char &Key)

{

  // Do something

}

 

{

  form = new TForm(nil);

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnKeyPress = &Combo1KeyPress;

}


 

OnKeyUp 

Occurs when the user releases a key that has been pressed.

 

(ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble) TShiftState;

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1KeyUp(TObject Sender, WORD &Key, TShiftState Shift)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnKeyUp = &Combo1KeyUp;

}


 

OnMouseEnter 

Occurs when the user moves the mouse into a control.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1MouseEnter(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnMouseEnter = &Combo1MouseEnter;

}


 

OnMouseLeave 

Occurs when the user moves the mouse outside of a control.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1MouseLeave(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnMouseLeave = &Combo1MouseLeave;

}


 

OnSelect 

Occurs when the user selects a string in the drop-down list.

 

Ex.

 

TForm form;

TComboBox Combo1;

void Combo1Select(TObject Sender)

{

  // Get the selected from the edit box

  String s = Combo1.Text;

 

  // Or get the selected text from the drop down list

  int n = Combo1.ItemIndex;

  String s = Combo1.Items[n];  

}

 

{

  form = new TForm(nil);

 

  Combo1 = TComboBox.Create(form);

  Combo1.Parent = form;

  Combo1.OnSelect = &Combo1Select;

}


 


 


 


 


 


 

 

 

 

 

Copyright © 2022 Rickard Johansson