views:

137

answers:

2

Edit: Update at bottom.

Hope someone can help here as it's driving me round the bend!

Delphi 2009

I have a form with two TComboxBoxEx components on it

One I populate at runtime with the following code

procedure TForm1.btn1Click(Sender: TObject);
var
  N: Integer;
begin
  cb1.ItemsEx.Add.Caption := 'Test';
  for N := 0 to 5 do
    with cb1.ItemsEx.Add do
    begin
      Caption := 'Item ' + IntToStr(N);
      Indent := 1;
    end;
 end;

The other I populate at design time using the same data and setting the same properties.

The items in the one I populate at runtime don't indent at all, while the design time ones indent just fine.

Any ideas? The help says that ident is the number of pixels to indent by but the design time ones are indented by more than one pixel even though indent is set to 1.

Setting indent to 10, for example, in the code above has no effect.

Here is the section of the DFM for the design time comobo

object cb2: TComboBoxEx
Left = 184
Top = 8
Width = 145
Height = 22
ItemsEx = <
  item
    Caption = 'Test'
  end
  item
    Caption = 'Item 0'
    Indent = 1
  end
  item
    Caption = 'Item 1'
    Indent = 1
  end
  item
    Caption = 'Item 2'
    Indent = 1
  end
  item
    Caption = 'Item 3'
    Indent = 1
  end
  item
    Caption = 'Item 4'
    Indent = 1
  end
  item
    Caption = 'Item 5'
    Indent = 1
  end>
ItemHeight = 16
TabOrder = 2
Text = 'cb1'
end

Update

Setting the Data property of the combo item after caption and indent seems to get it to work.

procedure TForm1.btn1Click(Sender: TObject);
var
  N: Integer;
begin
  cb1.ItemsEx.Add.Caption := 'Test';
  for N := 0 to 5 do
    with cb1.ItemsEx.Add do
    begin
      Caption := 'Item ' + IntToStr(N);
      Indent := 1;
      Data := Pointer(N);  // New Line
    end;
 end;

All a bit odd.

+1  A: 

Not quite sure why your code doesn't work, but here's some that does:

for N := 0 to 5 do
   cb1.ItemsEx.AddItem(intToStr(N), 0, 0, 0, DESIRED_INDENT_LEVEL, nil);
Mason Wheeler
Thanks, that worked. also see my update
Jamie
A: 

Try setting the Indent property before setting Caption. Seems to be a "known bug" in TComboBoxEx.

Hendrik