Programmatically force user to select an item in a TComboBox

LeTay
LeTay used Ask the Experts™
on
On a TForm I have a TComboBox component
I want to programmatically "click" on it so that the user has to select an item in the list
TComboBox.Click is refused by the compiler
Any other way ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
You can set the ItemIndex property to select a value. Using -1 clears the TComboBox and any value starting from 0 to the number of items in the TComboBox - 1 is valid (which can be determined using the property TComboBox.Items.Count)

If you want to force the user to select a value, you can check the ItemIndex value in the OnExit handler and set focus back to the combobox, but this is not particularly friendly as it could prevent the user from doing anything else on the form. Alternatively, you could set the colour or prompt the user. Depending on the type of combobox, ItemIndex could be -1 if the value in the editor (the "text" property) is not in the list.

So in summary, for a TComboBox named ComboBox1, to clear it use
  ComboBox1.ItemIndex := -1;

Open in new window

To select the first item use
  ComboBox1.ItemIndex := 0;

Open in new window

To select the last item, or clear it if there are no items use
  ComboBox1.ItemIndex := ComboBox1.Items.Count - 1;

Open in new window

Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
Having read your question again, I think you may be saying that you want to activate the TComboBox so that the user is placed on that control at run-time. If this is the case, use the SetFocus method. You can check that the control able to receive focus using the CanFocus property.

Author

Commented:
Well, that is not exactly what I need : I want to programmaticaly have the items listed, like if the user clicked on the V sign ...
Software Developer / Linux System Administrator / Managing Director
Commented:
OK. Just set the DroppedDown property to True. You may also want to call SetFocus as well to ensure that the TComboBox is activated.

Author

Commented:
Many thanks for you quick answer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial