我正在使用Delphi Prism for .NET。我需要从另一个winform方法中调用我的mainform类中的公共方法。所以,最近我了解了静态,我在程序中使用了它。静态或类的winform工程很好,但使一个方法静态或类似乎没有相同的工作。Delphi Prism中的静态或类方法
我有一个叫我MainForm类updateButtons方法。它根据用户的操作更新mainform上的所有按钮和控件。该方法需要从另一个winform方法中调用。所以,我将该UpdateButtons方法变为静态或类。虽然现在我看到要调用的方法,但编译器不喜欢。它不断提出以下错误,“无法调用实例成员(任何控件)没有实例引用。”
你怎么能做出一个方法从WinForm的类或静态的,仍然可以访问控制?
主要类的静态或类方法:updatebutton的
MainForm = partial class(System.Windows.Forms.Form)
private
protected
method Dispose(disposing: Boolean); override;
public
class method updateButtons;
end;
定义:
class method MainForm.updateButtons;
begin
if SecurityEnabled then
LoginBtn.Enabled := true //All the lines where I call Buttons raise the error exception that I mentioned above.
else
begin
UnitBtn.Enabled := true;
SignalBtn.Enabled := true;
AlarmBtn.Enabled := true;
MakerBtn.Enabled := true;
TrendBtn.Enabled := true;
DxCommBtn.Enabled := (Scanning = false);
TxBtn.Enabled := true;
ControlBtn.Enabled := true;
PIDBtn.Enabled := true;
SystemBtn.Enabled := true;
WinListBox.Enabled := true;
WinBtn.Enabled := true;
ShutdownBtn.Enabled := true;
OptionBtn.Enabled := true;
LoginBtn.Enabled:=false;
end;
end;
实际上,在这种情况下,如果你想从dxCommWin.Scan中调用它,你可以简单地将引用传递给MainForm(self)作为方法的第二个参数:''dxCommWin.Scan(不扫描,自我)在这种情况下,该方法的签名看起来像'method dxCommWin.Scan(scanning:boolean; form:MainForm);'并且您可以从该方法内访问表单。那就是我的意思是“检索对表单的引用”。 –
是的。我计划实现一个类似的代码,但我想到了我的解决方案,似乎更合适,因为这就是我想要做的 - 在调用dxCommWin.Scan之后立即执行该方法。谢谢,塞巴斯蒂安。 – ThN