0

Today, I'm using a lot these functions but I have already found several problems about them. My program is so structured:

This is Form 1:

public partial class Form1 : Form
{
 public Form1()
    {
        InitializeComponent();
        ...
        ...
        edit = new Button();
        edit.Click += new EventHandler((s, e) => edit_mode(s, e,x,y,c,z)); //x, y, c, z are some variables that I need to pass to purposes of this software.
        this.Controls.Add(edit);
    }
 void edit_mode(object sender, EventArgs e, string x,string y, int c, int z)
    {
        ...
        ...
        Form edits = new Form4();
        edits.Show();
        this.Hide();
    }
}

and this is Form 4:

public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
        ...
        ...
        ...
        Form backs= new Form1();
        backs.Show();
        this.Close();

    }

The target of this piece is the follow: when I enter in the "edit_mode" function, Form1 has to disappear to display a new Form (Form4).

I' ve searched a lot here, but no solutions worked for me: Compiler doesn't report me errors, so when I debug this piece of code all seems working; But when it arrives to edits.Show(); (in Form1) it says: ObjectDisposedException error and I don't understand why. I've tried to replace that this.Close() with this.Hide() and the program runs good until the this.Hide() function of Form4 is performed: In fact, when Form4 appears and performs its code it stops itself to this.hide() and the form4 doesn't disappear (while appear the Form1). I've tried also to create a Button in form4 and to move here the code to open the Form1.. and it works great! but I've only a problem: I don't want the user to press a button to make this working. So I need to make automatic this "Form change".

Any idea about this problem?

Ricks23
  • 11
  • 1
  • 2
  • Why do you need to create and display a form (Form4) and then trying to close it in its constructor? The code in the .... could not be replaced by a class method called directly by Form1? – Steve Aug 25 '15 at 22:30
  • In addition to what Steve said... you are creating a new instance of Form1. If you want to show the already initialized Form1 you will have to add the Owner property before showing Form4 : edits.Owner = this; and then to show it from Form4 you do this.Owner.Show(); – Jonathan Carroll Aug 25 '15 at 22:37
  • ObjectDisposedException occurs if you are trying to do some operation in a disposed object. After calling Close method you cannot call the Show method, because the form's resources have already been disposed. And why are you trying to close a form in the form constructor?? – ssakash Aug 25 '15 at 22:38

1 Answers1

2

Not sure what you are trying to achieve in the .... dots placed in the constructor of Form4, but for sure, calling Close at the end of the constructor will blows up the edits.Show called after the Form edits = new Form4();
Indeed, at that point, the edits variable points to an instance of Form4 that is no more usable (disposed).

You could try to fix your code with these changes

void edit_mode(object sender, EventArgs e, string x,string y, int c, int z)
{
    ...
    ...
    this.Hide();
    Form edits = new Form4();
    edits.ShowDialog();
    this.Show();
}


public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
        this.Shown += Form4_Shown;
        // These lines doesn't make sense because you are creating a 
        // different instance of Form1. This instance (named backs) is 
        // not the same instance that creates the Form4 
        // Form backs= new Form1();
        // backs.Show();

        // Moved to the Form4_Shown event handler
        // this.Close();

    }
    public void Form4_Shown(object sender, EventArgs e)
    {
        // Move here the code that was previously in the constructor
        ....
        ....
        this.Close();
    }

This approach move the code that you have replaced with dots from the constructor of Form4 to the Shown event. This event fires when the form is displayed and all the controls have been initialized. At that point you could decide to close the instance.

Still it is puzzling why you need to display a form and then close it immediately. If you don't need any interaction with the user you could choose to move the dotted code in a class and never try to display a Form4.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    This still has the same old problem that the Form is destroyed before it's finished being created, so `ShowDialog()` will fail. – Ben Voigt Aug 25 '15 at 22:40
  • @BenVoigt you are correct, need to move the code in the Shown event – Steve Aug 25 '15 at 22:41
  • Well, form1 contains several labels and buttons made using mysql data. if I want to edit and display correctly these data, I need to refresh (or better reboot) all application. But the problem is that I don't want close my application. So in order to reboot all application without closing it, I would like use Form4: it is a loading form.. So there is a var called I that is increased from 0 to 20 and finished it,I recall the form1 using backs.show() and a reboot occurrs. – Ricks23 Aug 25 '15 at 23:45