Delphi中的Combobox Style'csDropDownList'

nar*_*ren 1 delphi combobox delphi-7

我在delphi 7中创建了一个表单,并在其上添加了一个组合框.组合框包含项目列表.我不希望用户可以输入值到Combobox,所以我已经设置

combobox.style := csDropDownList;
Run Code Online (Sandbox Code Playgroud)

但我想使用的代码,combobox.text := 'New Item';但它不起作用.请注意,我想要显示的文本不在项目列表中,我不想在那里添加它.请问有什么解决方案吗?

And*_*and 5

不,这根本不是Windows组合框控件的工作方式.

但是,如果你坚持了,你不关心你的用户会感到困惑,您可以设置StylecsDropDown,然后做

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  Key := #0;
end;
Run Code Online (Sandbox Code Playgroud)

作为组合框的OnKeyPress事件.然后,用户无法手动输入文本,但只能从列表中的项目中进行选择.但是,仍然可以通过设置Text属性将文本设置为您喜欢的任何内容(即使它不在列表中):

ComboBox1.Text := 'Sample';
Run Code Online (Sandbox Code Playgroud)