Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:14:14+00:00 2026-05-23T22:14:14+00:00

I am programming under Delphi Prism for Mono. I keep getting this error, Cannot

  • 0

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; 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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:14:15+00:00Added an answer on May 23, 2026 at 10:14 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new in programming under linux and trying to get working this code:
I'm quite fond of using GNU getopt , when programming under Linux . I
I am learning socket programming under Linux,so I make a sample program to list
I am just starting programming ansi c with gcc under ubuntu (9.04). I get
I have a book about programming under Windows, and the author uses a function
As I understand IOCP under Windows Server 2003/2008 and C++ programming, they are more-or-less
I can't get VS2010 to auto-pop after i type a period. I'm programming in
Please forgive me if this question is ridiculous. I'm relatively new to programming and
Programming Student here...trying to work on a project but I'm stuck. The project is
Programming in vim I often go search for something, yank it, then go back

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.