Delphi - 将变量传递给另一种形式(没有全局变量)
Delphi - passing variables to another form (without global variables)
我指的是问题
Passing the variable to another Form
是否还有一种方法可以在不使用全局变量的情况下移交数据 - 例如从设置表单到应用程序的主表单?
既然你在谈论“设置表单”,我假设该表单是模态显示的。那么其实就差不多了。
例如,创建一个带有标签和按钮的新 VCL 应用程序:
然后创建一个设置窗体,用于设置中间主标签的字体。它看起来像这样,有两个 TLabel
控件、两个 TEdit
控件、两个 TCheckBox
控件和两个 TButton
控件。
不要忘记确保 Tab 键顺序正确,每个控件都有一个唯一的访问键(使用标签的 FocusControl
属性 连接到适当的编辑框),确定 按钮有 Default = True
和 ModalResult = mrOk
,取消 按钮有 Cancel = True
和 ModalResult = mrCancel
.
(作为奖励,在尺寸编辑框上设置 NumbersOnly = True
。)
现在,要在表单之间传递信息,就这么简单:
procedure TfrmMain.btnSettingsClick(Sender: TObject);
var
dlg: TfrmSettings;
begin
dlg := TfrmSettings.Create(Self);
try
// Populate dialog
dlg.eFont.Text := lblCaption.Font.Name;
dlg.eSize.Text := lblCaption.Font.Size.ToString;
dlg.cbBold.Checked := fsBold in lblCaption.Font.Style;
dlg.cbItalic.Checked := fsItalic in lblCaption.Font.Style;
if dlg.ShowModal = mrOk then
begin
// Apply settings from dialog
lblCaption.Font.Name := dlg.eFont.Text;
lblCaption.Font.Size := StrToInt(dlg.eSize.Text);
if dlg.cbBold.Checked then
lblCaption.Font.Style := lblCaption.Font.Style + [fsBold]
else
lblCaption.Font.Style := lblCaption.Font.Style - [fsBold];
if dlg.cbItalic.Checked then
lblCaption.Font.Style := lblCaption.Font.Style + [fsItalic]
else
lblCaption.Font.Style := lblCaption.Font.Style - [fsItalic];
end;
finally
dlg.Free;
end;
end;
设置窗体有几种可能性可以在不使用全局变量的情况下将数据移交给应用程序主窗体。我假设设置表单是由主表单创建的,如下所示:
SettingForm := TSettingForm.Create(Self);
SettingForm.ShowModal;
设置表单完成(关闭)后,ShowModal returns 和 mainform 可以访问设置表单的任何字段(变量)或 属性,然后销毁它:
ShowMessage(SettingForm.SomeVariable.ToString);
SettingForm.Free;
另一种方法是使用事件。
type
TSettingFormValueAvailableEvent = procedure (Sender : TObject; Value : Integer) of object;
// Create the form and assign an event handler then show the form
SettingForm := TSettingForm.Create(Self);
SettingForm.OnValueAvailable := SettingFormValueAvailable;
SettingForm.ShowModal;
// The event handler in main form
procedure TForm1.SettingFormValueAvailable(Sender: TObject; Value : Integer);
begin
ShowMessage(Value.ToString);
end;
// The event declaration in TFormSetting
private
FOnValueAvailable : TSettingFormValueAvailableEvent ;
public
property OnValueAvailable : TSettingFormValueAvailableEvent read FOnValueAvailable write FOnValueAvailable;
// The use of the event in the form setting
procedure TFormSetting.Button1.Click(Sender : TObject);
begin
if Assigned(FOnValueAvailable) then
FOnValueAvailable(Self, 1234); // Pass value 1234
end;
使用事件需要更多的代码,但它是“实时的”。主窗体在显示设置窗体的过程中,如果发生什么事情,主窗体可以立即做出反应。
我指的是问题 Passing the variable to another Form
是否还有一种方法可以在不使用全局变量的情况下移交数据 - 例如从设置表单到应用程序的主表单?
既然你在谈论“设置表单”,我假设该表单是模态显示的。那么其实就差不多了。
例如,创建一个带有标签和按钮的新 VCL 应用程序:
然后创建一个设置窗体,用于设置中间主标签的字体。它看起来像这样,有两个 TLabel
控件、两个 TEdit
控件、两个 TCheckBox
控件和两个 TButton
控件。
不要忘记确保 Tab 键顺序正确,每个控件都有一个唯一的访问键(使用标签的 FocusControl
属性 连接到适当的编辑框),确定 按钮有 Default = True
和 ModalResult = mrOk
,取消 按钮有 Cancel = True
和 ModalResult = mrCancel
.
(作为奖励,在尺寸编辑框上设置 NumbersOnly = True
。)
现在,要在表单之间传递信息,就这么简单:
procedure TfrmMain.btnSettingsClick(Sender: TObject);
var
dlg: TfrmSettings;
begin
dlg := TfrmSettings.Create(Self);
try
// Populate dialog
dlg.eFont.Text := lblCaption.Font.Name;
dlg.eSize.Text := lblCaption.Font.Size.ToString;
dlg.cbBold.Checked := fsBold in lblCaption.Font.Style;
dlg.cbItalic.Checked := fsItalic in lblCaption.Font.Style;
if dlg.ShowModal = mrOk then
begin
// Apply settings from dialog
lblCaption.Font.Name := dlg.eFont.Text;
lblCaption.Font.Size := StrToInt(dlg.eSize.Text);
if dlg.cbBold.Checked then
lblCaption.Font.Style := lblCaption.Font.Style + [fsBold]
else
lblCaption.Font.Style := lblCaption.Font.Style - [fsBold];
if dlg.cbItalic.Checked then
lblCaption.Font.Style := lblCaption.Font.Style + [fsItalic]
else
lblCaption.Font.Style := lblCaption.Font.Style - [fsItalic];
end;
finally
dlg.Free;
end;
end;
设置窗体有几种可能性可以在不使用全局变量的情况下将数据移交给应用程序主窗体。我假设设置表单是由主表单创建的,如下所示:
SettingForm := TSettingForm.Create(Self);
SettingForm.ShowModal;
设置表单完成(关闭)后,ShowModal returns 和 mainform 可以访问设置表单的任何字段(变量)或 属性,然后销毁它:
ShowMessage(SettingForm.SomeVariable.ToString);
SettingForm.Free;
另一种方法是使用事件。
type
TSettingFormValueAvailableEvent = procedure (Sender : TObject; Value : Integer) of object;
// Create the form and assign an event handler then show the form
SettingForm := TSettingForm.Create(Self);
SettingForm.OnValueAvailable := SettingFormValueAvailable;
SettingForm.ShowModal;
// The event handler in main form
procedure TForm1.SettingFormValueAvailable(Sender: TObject; Value : Integer);
begin
ShowMessage(Value.ToString);
end;
// The event declaration in TFormSetting
private
FOnValueAvailable : TSettingFormValueAvailableEvent ;
public
property OnValueAvailable : TSettingFormValueAvailableEvent read FOnValueAvailable write FOnValueAvailable;
// The use of the event in the form setting
procedure TFormSetting.Button1.Click(Sender : TObject);
begin
if Assigned(FOnValueAvailable) then
FOnValueAvailable(Self, 1234); // Pass value 1234
end;
使用事件需要更多的代码,但它是“实时的”。主窗体在显示设置窗体的过程中,如果发生什么事情,主窗体可以立即做出反应。