SRL Archive - General

Tcombobox disabling options


StevenV

2011-08-18 12:31:56

hello,

i want to make a combobox with some sort of labels inside it.
So it has to contain some options which can not be clicked.

example:

i want a combobox with options
fruit**
apple
banana
..
vegtable**
leek
tomato
…


** must be a label and so not clickable.

Flight

2011-08-18 12:37:55

Ask BraK, our 'Forms Guru". ;)

Coh3n

2011-08-18 12:44:53

There's no property for it, but here a sort of "hack" that will work. You can technically click the options, but they don't show in the combo box. Just run this and try it out.

program formTemplate;

var
form: TForm;

procedure disableComboClick(sender: TObject);
begin
TComboBox(sender).text := '';
end;

procedure formInit;
begin
form := TForm.create(nil);
with form do
begin
caption := 'Form Template';
setBounds(100, 100, 500, 300);
end;

with TComboBox.create(form) do
begin
parent := form;
setBounds(10, 10, 100, 25);
items.add('fruit:');
items.add(' apple');
items.add(' banana');
items.add('vegetable');
items.add(' leep');
items.add(' tomato');
onChange := @disableComboClick;
end;

form.showModal;
end;

procedure formSafeCall(proc: string);
var
v: TVariantArray;
begin
setLength(v, 0);
threadSafeCall(proc, v);
end;

procedure formFree();
begin
form.free();
end;

begin
clearDebug();

try
formSafeCall('formInit');
except
writeln(exceptionToString(exceptionType, exceptionParam));
finally
formSafeCall('formFree');
end;
end.

BraK

2011-08-18 14:00:08

Coh3n that is kinda "Hack" I kinda like it though. You might wanna have it check what the text is before setting it to '' though. :p I'll look into it when I get the chance I've got a few big project going at the moment.

~BraK

E: @Flight One project is the SRL player form for your scripts really only one feature that's going to/is holding me up.

Freddy1990

2011-08-19 12:58:25

There's indeed no "normal" way to do this with a standard combobox, I would suggest you prefix the "disabled" items with something like [DIS] or w/e and then if they are clicked, just select the previously selected/random or no item to prevent them from being used.

EDIT: Rather than prefixing the items, you could for example draw them with grey text: http://www.delphipages.com/forum/showthread.php?t=191561

BraK

2011-08-20 09:28:58

The only thing I could really come up with that was viable was to reset the Itemindex to -1 after someone clicks it. The code below shows the easy way.

program formTemplate;

var
Form: TForm;
TCom: TCOMBOBOX;

procedure Change(Sender: TObject);
begin
if InIntArray([0, 4], Tcom.ITEMINDEX) then
Tcom.ITEMINDEX := -1;
end;

procedure formInit;
begin
form := TForm.create(nil);
with form do
begin
caption := 'Form Template';
SetBounds(100, 100, 115,40);
end;

TCom := TCOMBOBOX.Create(Form);
with TCom do
begin
SetBounds(5,5,100,30);
Parent := Form;
FONT.Color := clGray;
CANVAS.Font.Color
ITEMS.Add('----Fruit----');
ITEMS.Add('Apple');
ITEMS.Add('Pear');
ITEMS.Add('Orange');
ITEMS.Add('----Veggie----');
ITEMS.Add('Carrot');
ITEMS.Add('Potato');
ITEMS.Add('Mushroom');
ITEMS.Add('Bellpepper');
ONCHANGE := @Change;
end;

form.showModal;
end;

procedure formSafeCall(proc: string);
var
v: TVariantArray;
begin
setLength(v, 0);
threadSafeCall(proc, v);
end;

procedure formFree();
begin
form.free();
end;

begin
clearDebug();

try
formSafeCall('formInit');
except
writeln(exceptionToString(exceptionType, exceptionParam));
finally
formSafeCall('formFree');
end;
end.

~BraK