Cannot access a disposed object in Delphi Prism
I am programming under Delphi Prism for Mono. I keep getting this error, "Cannot access a disposed object" after opening already opened and closed window form, even though the window form is declared within a class and instantiated in the constructor as it follows.
Here is the Class in which it is declared:
MainForm = partial class(System.Windows.Forms.Form)
private
method SignalBtn_Click(sender: System.Object; e: System.EventArgs);
method CommBtn_Click(sender: System.Object; e: System.EventArgs);
method button1_Click(sender: System.Object; e: System.EventArgs);
method button2_Click(sender: System.Object; e: System.EventArgs);
method button4_Click(sender: System.Object;开发者_StackOverflow社区 e: System.EventArgs);
method button5_Click(sender: System.Object; e: System.EventArgs);
method MainForm_Load(sender: System.Object; e: System.EventArgs);
method ShutdownBtn_Click(sender: System.Object; e: System.EventArgs);
method MySerialData(sender: System.Object; e:SerialDataReceivedEventArgs);
method LoginBtn_Click(sender: System.Object; e: System.EventArgs);
protected
method Dispose(disposing: Boolean); override;
public
RX:Array[0..5] of byte;
TX:Array[0..6] of byte;
serialPort1:System.IO.Ports.SerialPort;
thr:Thread;
stoploop:Boolean;
mcommand:Byte;
thechannel:Integer;
fr : UnitForm; <<<<<<<<<<<< Here is the form being declared.
constructor;
method FillTable;
method mythread;
end;
Here is the contructor where it is instantiated or created:
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
mcommand:=$AA;
thechannel:=$01;
stoploop:=false;
thr:=nil;
fr := new UnitForm; <<<<<<<<<<<<<<<<<< fr is created as new.
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
Here is how the form is shown or opened within a button event:
method MainForm.UnitBtn_Click(sender: System.Object; e: System.EventArgs);
begin
fr.Show;
end;
Being that fr is a window form that is part of the class much like a global variable, why is the window form disposed when you close it after opening it. Isn't it kept in the memory until the whole program is shutdown for easy access?
UPDATE: Within onFormClosing, I hide the form and it seems to work, but when I try to reopen the form again, it raises the same exception as before "Cannot access disposed object."
method UnitForm.UnitForm_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
hide;
end;
When you look into the documentation of System.Windows.Forms.Form.Close() you will notice, that it's documented that the Form frees all it's resources and disposes itself when closed.
What you want is actually Form.Hide() to hide the form from the user.
Edit update: You would call .Close() on the Form only when you would call Free / FreeAndNil in native Delphi. That's sort of the same thing.
For those who needs to actually know how to do it, here is the example code:
method Form2.Form2_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
e.Cancel:=true; //line prevents form resources being released
self.hide; //line tells the form to disappear.
end;
精彩评论