Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

combobox with disabled items?

1,248 views
Skip to first unread message

jodleren

unread,
Jan 20, 2010, 9:20:03 AM1/20/10
to
Hi!

Is there a way for doing this?

I can draw items as i want, but having items not selectable? Is that
possible?

WBR
Sonnich

alang...@aol.com

unread,
Jan 21, 2010, 9:14:41 AM1/21/10
to

const
CB1DisabledIndices = [2, 5];

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if (ComboBox1.ItemIndex in CBDisabledIndices) then
ComboBox1.ItemIndex := -1;
end;

This uses a set for multiple disabling, but if you have only one
then . . .

const
CB1DisabledIndex = 2;

if (ComboBox1.ItemIndex = CBDisabledIndex) then
etc

And when you draw the items if they're disabled then draw them a
different colour.

Alan Lloyd

BRoberts

unread,
Jan 21, 2010, 8:17:51 PM1/21/10
to
<alang...@aol.com> wrote in message
news:2bcf49a9-e5f4-4396...@22g2000yqr.googlegroups.com...


An alternative to using a separate structure to track item status would be
to use the Items.Objects property:

ComboBox1.Items.Objects [n] := tObject (EnabledValueOfItemN)

or

ComboBox1.Items.AddObject (ItemCaption, tObject (ItemEnabledState))

then

if Boolean (ComboBox1.Items.Object [n]) then // it's enabled

An alternative to setting the ItemIndex to -1 when a disabled item is
selected would be to track the previous value:

In onClick

if not Boolean (ComboBox1.Items.Objects [Combobox1.ItemIndex])
then ComboBox1.ItemIndex := cb1LastIndex;
cbLastIndex := ComboBox1.ItemIndex;

where cbLastIndex is declared in the private section of the form as an
integer.

alang...@aol.com

unread,
Jan 22, 2010, 5:17:10 AM1/22/10
to
On 22 Jan, 01:17, "BRoberts" <berdontem...@caneris.ca> wrote:
>
> An alternative to using a separate structure to track item status would be
> to use the Items.Objects property:
>
> � � ComboBox1.Items.Objects [n] := tObject (EnabledValueOfItemN)
>
> or
>
> � � ComboBox1.Items.AddObject (ItemCaption, tObject (ItemEnabledState))
>
> then
>
> � � if Boolean (ComboBox1.Items.Object [n]) then // it's enabled
>

A good idea, particularly if the disabled items are set dynamically
during program interaction.

OTOH if there are more than 1 disabled items, or if they have to be
set to a default, I think using a set and setting the Objects[] in a
loop is clearer.

DisabledItems = [3, 5, 7];

for i := 0 to MyComboBox.items.Count - 1 do
if i in DisabledItems then . . .

> An alternative to setting the ItemIndex to -1 when a disabled item is
> selected would be to track the previous value:
>
> In onClick
>
> � � if not Boolean (ComboBox1.Items.Objects [Combobox1.ItemIndex])
> � � then ComboBox1.ItemIndex := cb1LastIndex;
> � � cbLastIndex := ComboBox1.ItemIndex;
>
> where cbLastIndex is declared in the private section of the form as an
> integer.

Good technique.

Alan Lloyd

0 new messages