How put 1st item (index 0) of ComboBox as already selected?
procedure TForm1.FormCreate(Sender: TObject);
begin
with ComboBox1.Items do
begin
Add('1st Item');
Add('2nd Item');
Add('3rd Item');
end;
end;
// PS: Change the Style property of ComboBox1 to csOwnerDrawFixed
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
AnIcon: TIcon;
begin
AnIcon := TIcon.Create;
try
ImageList1.GetIcon(Index, AnIcon);
with Control as TComboBox do
begin
Canvas.Draw(Rect.Left, Rect.Top, AnIcon);
Canvas.TextOut(Rect.Left + ImageList1.Width, Rect.Top, Items[Index]);
end;
finally
AnIcon.Free;
end;
end;
Just set ItemIndex
to 0:
procedure TForm1.FormCreate(Sender: TObject);
begin
with ComboBox1.Items do
begin
Add('1st Item');
Add('2nd Item');
Add('3rd Item');
end;
ComboBox1.ItemIndex := 0;
end;
I have left the with
clause intact, but as an aside I am not a great fan of them.
I would just point out a "gotcha". If you set ItemIndex
before adding any items, it won't work because there is no item 0 yet, but it won't throw an error either.