具有未知ItemIndex的组合框项目选择

HP *_*ner 0 delphi

我是Delphi的新手.我有一个Delphi XE2程序.我ComboBox1在表单创建过程中创建如下:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.BeginUpdate;
  ComboBox1.Items.Clear;
  ComboBox1.Items.Add('BBBB');
  ComboBox1.Items.Add('DDDD');
  ComboBox1.Items.Add('AAAA');
  ComboBox1.Items.Add('CCCC');
  ComboBox1.Items.EndUpdate;
end;   
Run Code Online (Sandbox Code Playgroud)

......这里是ComboBox1属性:

Sorted = True
OnChange = ComboBox1Change
OnDropDown = ComboBox1DropDown
Run Code Online (Sandbox Code Playgroud)

我的要求是在项目的选择,做了一些工作,使用case of,记住,我不知道ItemIndexAAAA ...... DDDD等.

所以我尝试了以下方法:

case ComboBox1.ItemIndex of ComboBox1.Items.IndexOf('AAAA'):
  begin
    //
    //
  end
end;

case ComboBox1.ItemIndex of ComboBox1.Items.IndexOf('BBBB'):
  begin
    //
    //
  end
end;

case ComboBox1.ItemIndex of ComboBox1.Items.IndexOf('CCCC'):
  begin
    //
    //
  end
end;

case ComboBox1.ItemIndex of ComboBox1.Items.IndexOf('DDDD'):
  begin
    //
    //
  end
end;
Run Code Online (Sandbox Code Playgroud)

我的项目没有编译.它给出了如下错误:

[DCC Error] Unit1.pas(....): E2026 Constant expression expected 
Run Code Online (Sandbox Code Playgroud)

另一个问题是:Delphi //{}Delphi有什么区别?基本上我可以通过使用//和编写任何评论来理解我的程序{}吗?

Ken*_*ite 6

case仅适用于序数(整数)类型和常量表达式.请改用几个if语句.

var
  SelectedItem: string;

begin
  SelectedItem := '';
  if ComboBox1.ItemIndex <> -1 then
    SelectedItem := ComboBox1.Items[ComboBox1.ItemIndex];

  // Or you can just exit if ComboBox1.ItemIndex is -1
  // If ComboBox1.ItemIndex = -1 then
  //   Exit;
  if SelectedItem = 'AAAA' then
  begin

  end
  else if SelectedItem = 'BBBB' then
  begin

  end
  else if SelectedItem = 'CCCC' then
  begin

  end
  else if SelectedItem = 'DDDD' then
  begin

  end;
end;
Run Code Online (Sandbox Code Playgroud)

至于{}和之间的区别//,第一个可以包装多行注释,而第二个只是单行注释.

{ 
  This is a multiple line comment
  between curly braces.
}

// This is a single line comment. If I want to exend it
// to a second line, I need another single line comment
Run Code Online (Sandbox Code Playgroud)

还有另一个多行评论指标,从旧的Pascal日延续:

(*
  This is also a multiple line comment
  in Delphi.
  {
  It is useful to surround blocks of code that
  contains other comments already.
  }
  This is still a comment here.
*)
Run Code Online (Sandbox Code Playgroud)