如何从没有按钮的表单中模拟模态结果?
How can I emulate a modal result from a form which has no buttons?
我有一个表单,它将以模态方式显示,上面只有一个列表框。我希望能够 return 双击列表框中的项目时 mrOK 的模态结果或用户按转义键时 mrCancel 的模态结果。
当表单上没有按钮来分配模态响应时,我该如何模拟呢?
这其实很简单:只需设置表单的ModalResult
属性:
Use ModalResult to close the form when it is displayed modally.
By default, ModalResult is mrNone. Set ModalResult to any nonzero value to close the form. The value assigned to ModalResult becomes the return value of the ShowModal function call used to display the form.
在你的情况下,
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
if ListBox1.ItemIndex <> -1 then
ModalResult := mrOk;
end;
procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_RETURN:
if ListBox1.ItemIndex <> -1 then
ModalResult := mrOk;
VK_ESCAPE:
ModalResult := mrCancel;
end;
end;
我有一个表单,它将以模态方式显示,上面只有一个列表框。我希望能够 return 双击列表框中的项目时 mrOK 的模态结果或用户按转义键时 mrCancel 的模态结果。
当表单上没有按钮来分配模态响应时,我该如何模拟呢?
这其实很简单:只需设置表单的ModalResult
属性:
Use ModalResult to close the form when it is displayed modally.
By default, ModalResult is mrNone. Set ModalResult to any nonzero value to close the form. The value assigned to ModalResult becomes the return value of the ShowModal function call used to display the form.
在你的情况下,
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
if ListBox1.ItemIndex <> -1 then
ModalResult := mrOk;
end;
procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_RETURN:
if ListBox1.ItemIndex <> -1 then
ModalResult := mrOk;
VK_ESCAPE:
ModalResult := mrCancel;
end;
end;