如何将结果编码到组合框中的选择



我在Delphi 10.3中有一个TComboBox。我有一个包含30多个项目的组合框。我需要为组合框的每个项目编写不同的操作。目前我正在使用if-else语句。由于有30个不同的项目,if语句太长了。有更快的方法吗?

完全取决于您的情况。如果不知道你的确切情况,回答你的问题几乎是不可能的。

尽管如此,这里还是有一些想法。也许它们与你的处境有关,也许它们不是。

按索引进行的琐碎参数化

在最好的情况下,你的30个动作可以参数化。例如,假设组合框中的项目是

Show 1
Show 10
Show 100
Show 1000
...

其将显示具有给定号码的消息框。在这种情况下,您不需要30个不同的过程(这里每个过程都由对ShowMessage的简单调用表示(:

procedure TForm1.btnNextClick(Sender: TObject);
begin
case ComboBox1.ItemIndex of
0:
ShowMessage('1');
1:
ShowMessage('10');
2:
ShowMessage('100');
3:
ShowMessage('1000');
// ...
end;
end;

相反,您应该只使用一个过程,但要使用一个参数:

procedure TForm1.btnNextClick(Sender: TObject);
begin
if ComboBox1.ItemIndex <> -1 then
ShowMessage(IntPower(10, ComboBox1.ItemIndex).ToString)
end;

关联对象的参数化

如果不能单独通过项的索引来描述操作,则可以使用与每个项关联的对象指针。也许用它来存储一个整数就足够了:

procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.BeginUpdate;
try
ComboBox1.Items.Clear;
ComboBox1.Items.AddObject('Show 51', TObject(51));
ComboBox1.Items.AddObject('Show 111', TObject(111));
ComboBox1.Items.AddObject('Show 856', TObject(856));
ComboBox1.Items.AddObject('Show 1000', TObject(1000));
finally
ComboBox1.Items.EndUpdate;
end;
end;
procedure TForm1.btnNextClick(Sender: TObject);
begin
if ComboBox1.ItemIndex <> -1 then
ShowMessage(Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]).ToString);
end;

否则,你可以让它成为一个真正的指针,指向任何数据量的对象(整数,字符串,…(

不相关的程序

上面的例子都要求程序可以参数化,即用一个带有参数的单个程序代替。如果不是这样,如果程序完全无关,则需要不同的方法。但是,哪种方法最合适取决于你的确切情况。

下面是几个例子。

简单案例陈述

在设计时,将项设置为Play soundRun NotepadShow Start Menu

procedure PlaySound;
begin
MessageBeep(MB_ICONINFORMATION);
end;
procedure RunNotepad;
begin
ShellExecute(Form1.Handle, nil, 'notepad', nil, nil, SW_SHOWNORMAL)
end;
procedure ShowStartMenu;
begin
Form1.Perform(WM_SYSCOMMAND, SC_TASKLIST, 0)
end;
procedure TForm1.btnNextClick(Sender: TObject);
begin
case ComboBox1.ItemIndex of
0:
PlaySound;
1:
RunNotepad;
2:
ShowStartMenu;
end;
end;

将过程指针与项一起存储

procedure PlaySound;
begin
MessageBeep(MB_ICONINFORMATION);
end;
procedure RunNotepad;
begin
ShellExecute(Form1.Handle, nil, 'notepad', nil, nil, SW_SHOWNORMAL)
end;
procedure ShowStartMenu;
begin
Form1.Perform(WM_SYSCOMMAND, SC_TASKLIST, 0)
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.BeginUpdate;
try
ComboBox1.Items.Clear;
ComboBox1.Items.AddObject('Play sound', TObject(@PlaySound));
ComboBox1.Items.AddObject('Run notepad', TObject(@RunNotepad));
ComboBox1.Items.AddObject('ShowStartMenu', TObject(@ShowStartMenu));
finally
ComboBox1.Items.EndUpdate;
end;
end;
procedure TForm1.btnNextClick(Sender: TObject);
begin
if ComboBox1.ItemIndex <> -1 then
TProcedure(ComboBox1.Items.Objects[ComboBox1.ItemIndex])();
end;

好处:没有混淆指数的风险;动作是";附";到项目。

使用命令词典

也许您的应用程序有一组全局命令,由英文单词表示。然后,您可能需要使用字典来获取与单词相关联的过程。这也可以用于组合框。在设计时,设有三项:beepwritestart:

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
btnNext: TButton;
procedure btnNextClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FCommands: TDictionary<string, TProcedure>;
public
end;
procedure PlaySound;
begin
MessageBeep(MB_ICONINFORMATION);
end;
procedure RunNotepad;
begin
ShellExecute(Form1.Handle, nil, 'notepad', nil, nil, SW_SHOWNORMAL)
end;
procedure ShowStartMenu;
begin
Form1.Perform(WM_SYSCOMMAND, SC_TASKLIST, 0)
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FCommands := TDictionary<string, TProcedure>.Create;
FCommands.Add('beep', PlaySound);
FCommands.Add('write', RunNotepad);
FCommands.Add('start', ShowStartMenu);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FCommands.Free;
end;
procedure TForm1.btnNextClick(Sender: TObject);
var
Cmd: TProcedure;
begin
if
(ComboBox1.ItemIndex <> -1)
and
FCommands.TryGetValue(ComboBox1.Items[ComboBox1.ItemIndex], Cmd)
then
Cmd();
end;

最新更新