我正在使用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