0
votes

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;
2

2 Answers

1
votes

This cannot work in the way you want it to work.

A class (or static) method is called statically on the class, opposed to be called on a specific object instance.

You can instanciate the same form class several times. Then you have several object instances of the form, which can be opened or hidden all at the same time.

Now, when you call the static method, WHICH of those several forms should be updated? The compiler cannot tell, and can't allow access to fields or properties belonging to the object's instances.

For this to work, you must make the method a normal method of the object (non-class or static) and you need to retrieve a reference of the concrete form object instance and call it there.

0
votes

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.

//This is just a sample code
MainForm = class(system.windows.forms.form)
private
    method ScanBtn_Click(sender: System.Object; e: System.EventArgs);
protected
public
    Method UpdateButtons;
end;

Method Mainform.UpdateButtons;
begin
   Button1.enabled:=true;
   Button1.text:='Start Scanning';
end;

method MainForm.ScanBtn_Click(sender: System.Object; e: System.EventArgs);
begin
    if not Scanning then
        stopthread:=true;

    dxCommWin.Scan(not Scanning);
    UnitWin.UpdateMenu;  
    UpdateButtons; <-------I call it here instead of from dxCommWin.Scan Method.
end;