Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何将组合框的第一项设置为已选中?_Delphi_Combobox_Delphi 10.3 Rio - Fatal编程技术网

Delphi 如何将组合框的第一项设置为已选中?

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

如何将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 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?阿门“带”。