Results 1 to 6 of 6

Thread: Tcombobox disabling options

  1. #1
    Join Date
    Aug 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default Tcombobox disabling options

    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.

  2. #2
    Join Date
    Aug 2007
    Location
    Colorado
    Posts
    7,421
    Mentioned
    268 Post(s)
    Quoted
    1442 Post(s)

    Default

    Ask BraK, our 'Forms Guru".

    Current projects:
    [ AeroGuardians (GotR minigame), Motherlode Miner, Blast furnace ]

    "I won't fall in your gravity. Open your eyes,
    you're the Earth and I'm the sky..."


  3. #3
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    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.
    Simba Code:
    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.

  4. #4
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    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. 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.

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  5. #5
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    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/sho...d.php?t=191561
    Last edited by Freddy1990; 08-19-2011 at 05:07 PM.

  6. #6
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    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.

    Simba Code:
    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

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •