Programmatically force user to select an item in a TComboBox

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 ?
LVL 1
LeTayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martyn SpencerManaging DirectorCommented:
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

0
Martyn SpencerManaging DirectorCommented:
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.
0
LeTayAuthor 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 ...
0
Martyn SpencerManaging DirectorCommented:
OK. Just set the DroppedDown property to True. You may also want to call SetFocus as well to ensure that the TComboBox is activated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeTayAuthor Commented:
Many thanks for you quick answer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.