Delphi 如何将组合框的第一项设置为已选中?
如何将ComboBox的第一项(索引0)放在已选中的位置Delphi 如何将组合框的第一项设置为已选中?,delphi,combobox,delphi-10.3-rio,Delphi,Combobox,Delphi 10.3 Rio,如何将ComboBox的第一项(索引0)放在已选中的位置 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 csOwnerDra
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;
只需将
ItemIndex
设置为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;
我保留了和
子句,但作为旁白,我不是他们的超级粉丝
我只想指出一个“明白了”。如果您在添加任何项之前设置了
ItemIndex
,它将不起作用,因为还没有项0,但也不会引发错误。您是否尝试过ComboBox1.ItemIndex:=0代码>?阿门“带”。