combobox itemindex not being set when droppeddown is true

938 views
Skip to first unread message

Kerry Frater

unread,
May 1, 2008, 4:02:58 AM5/1/08
to
Hi,

Using D7.

I have a number of ComboBoxes on a form. When the user "enters" the box I
want to auto drop the box so the use can see the list and choose. I want to
make sure that the "Text" value of the combobox is exactly as it is in the
list. I also want the user to be able to tab out of the box without changing
the default value.

To achieve this:
1. I use the OnEnter event
TComboBox(Sender).DroppedDown := true; //this does mean that an item
MUST be selected before the user can leave the focus of the control

2. I use the exit event to set the text value of the box
if TComboBox(Sender).ItemIndex >= 0 then
TComboBox(Sender).Text :=
TComboBox(Sender).Items[TComboBox(Sender).ItemIndex];

3. I monitor the the keydown to check for the tab key if pressed whilst
droppeddown is true
if (Key = VK_TAB) then begin
TComboBox(Sender).DroppedDown := false;
if ssShift in Shift then
SelectNext(TComboBox(Sender),false,true)
else
SelectNext(TComboBox(Sender),true,true);
end;

The above routines are then common to all my comboboxes on the form.

This works for selecting a drop down item by mouse click, selecting an item
using the navigation keys.

It does NOT work if I select the item using the "first letter" of the item
followed by the return key. e.g. my dropped down list has "No Number"
(default), "Home Number", "Work Number" and "Mobile Number".
SO I enter the box in its default state.
Whilst the list is "droppeddown" I press "w" and hit the Enter key.
The ComboBox shows the correct value of "Work Number" with the correct
spelling (captial W).
I then press the tab key and the value changes to the original value "No
Number" i.e. the "OnExit" kicks in and sets the value referred to by the
ItemIndex which in this examle is "0".

This implies that whilst the use of the alpha keysto select with the Enter
key changes the Text value of the ComboBox it does not change the ItemIndex.

Is there a way of setting the ItemIndex correctly?

Kerry


Peter Below (TeamB)

unread,
May 1, 2008, 5:48:13 AM5/1/08
to
Kerry Frater wrote:

> Hi,
>
> Using D7.
>
> I have a number of ComboBoxes on a form. When the user "enters" the
> box I want to auto drop the box so the use can see the list and
> choose. I want to make sure that the "Text" value of the combobox is
> exactly as it is in the list. I also want the user to be able to tab
> out of the box without changing the default value.

You seem to be doing a lot of unnecessary work here. If you do want
your users to only select an item from the dropdown set the TComboboxes
Style to csDropdownlist and it will work as you want without any
cumbersome code.

--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com

Kerry Frater

unread,
May 1, 2008, 6:48:36 AM5/1/08
to

Thanks for the comment Peter.

Unfortunately, that property doesn't change any of the comments I made.

I use the combobox to save space on the screen. When focus goes to the
combobox I want the box to autodrop. In my D7 version that does not seem to
happen unles I program it in. Hence the "OnEnter" code.

The problem with the dropped down list is that you can't exit the WinControl
until you choose something (so I have to click or press enter key, which
does not move onto the next TabStop object). To allow a user to exit the
control with a single keystroke I programmed to monitor for the tabkey so
that I switch off the droppeddown list then I can move to the next one in
the sequence.

This works fine except when the item in the dropped down list is selcted
using the alphanumeric keys e.g. "W" when it appears to work except that
selecting using this key doesn't change the itemindex.

So your property means that I can remove the OnExit call


if TComboBox(Sender).ItemIndex >= 0 then
TComboBox(Sender).Text :=
TComboBox(Sender).Items[TComboBox(Sender).ItemIndex];

but it doesn't fix the problem. When you exit the boix the itemindex is not
set.
If I select using the "letter" then press "Enter" then exit, it works. I am
trying to work out how to do it without that extra keystroke.

Kerry


"Peter Below (TeamB)" <no...@nomail.please> wrote in message
news:xn0fpnxj...@newsgroups.borland.com...

Peter Below (TeamB)

unread,
May 1, 2008, 4:07:14 PM5/1/08
to
Kerry Frater wrote:

>
> Thanks for the comment Peter.
>
> Unfortunately, that property doesn't change any of the comments I
> made.
>
> I use the combobox to save space on the screen. When focus goes to the
> combobox I want the box to autodrop. In my D7 version that does not
> seem to happen unles I program it in. Hence the "OnEnter" code.
>
> The problem with the dropped down list is that you can't exit the
> WinControl until you choose something

Of course you can, hit the ESC key or Alt-up arrow, or just click
somewhere outside the dropdown window.

What the user cannot do is to get the control back to the "nothing
selected" state. That is the reason why you usually see pograms using
an entry in the list (usually the first) with a text like "(none)" or
simply a blank line (an item containing a single space character, for
example).

vieira....@gmail.com

unread,
Feb 21, 2013, 11:07:56 AM2/21/13
to
Hi Kerry,

Setting AutoDropDown to True solved the problem to me.

Att.,

Edilson.
Reply all
Reply to author
Forward
0 new messages