I am working with Delphi Prism for .NET. I need to call a public method in my mainform class from another winform method. So, having recently learned about static, I used it in my program. Static or Class winform works great, but making a method static or class doesn’t seem to work the same.
I have a method called updateButtons in my mainform class. It updates all the buttons and controls on the mainform according to the user’s action. This method needs to be called from another winform method. So, I made that UpdateButtons method into static or class. Although now I see the method to call, compiler doesn’t like. It keeps raising the following error, “Cannot call instance member (Any controls) without an instance reference.”
How can you make a method a class or static and still have access to controls from the winform?
Main class with static or class method:
MainForm = partial class(System.Windows.Forms.Form)
private
protected
method Dispose(disposing: Boolean); override;
public
class method updateButtons;
end;
definition of updatebutton:
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;
Since the method I want to execute is from MainForm Window Form and fired from within a button event, I decided to call that method from within the Button Click event from MainForm instead of from other winform. This has the same end result. Plus, it is simpler.