mahesh
mahesh

Reputation: 1390

Establish Owner On New Instance Of Form

Is it possible for establish owner on new instance of forms?. While on Working with main form and model windows this question arise in my mind that suppose if I create a new instance of Form1 as below:

//this Instance From main window
CashDeposit cd=new CashDeposit();
cd.Show(this);

Now I am going to close the same and trying to make the new instance of the same on new EventHandller of the CashDeposit as below:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 this.Close();
 CashDeposit cdd = new CashDeposit();
 cdd.Show();
}
//this would showing without any owner but if I create the new instance on another way  like below:        

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 this.Close();
 CashDeposit cdd = new CashDeposit();
 cdd.Show(this);
}

//than obviously it will going to fire the error like not creating owner on disposing  object or control etc.

So It is hard for me to eastablish owner of new instance of CashDeposit from same class because the reference form is disposing and don’t know how to eastablish the new relation between both the main window form and CashDeposit from the Class of CashDeposit on new instance of the same.

Here the mainform is the owner of CashDeposit. And I am trying to eastablish owner on new instance of CashDeposit after the disposing the Old One(Relational) Form as above.

Any one have idea about how to achieve the same?.

Upvotes: 0

Views: 192

Answers (3)

mahesh
mahesh

Reputation: 1390

Here I asked the question about establish the Main Form as a Owner into CashDeposit Class on New Instance of the same at certain EventHandller It may be Button Or TextboxKeyPress.

Look At Following Code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   this.Close();
   CashDeposit cdd = new CashDeposit();
   cdd.Show(this.Owner);
}

The above Code In CashDeposit Class Eastblished as a Owner of a self Instance where as I want to Eastablish the main form as a onwer of CashDeposit on New Instance Hence I am prefere to go with below code which solved the matters.

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      this.Close();
      CashDeposit cdd = new CashDeposit();
      cdd.Show(MainForm.ActiveForm); //You can Replcae MainForm with your Orginal Form 

    }

Now As per above I just added Form.ActiveForm Property which shows the owner of the activeform and handle the Main Windows Form and Model Window Forms very well.

Upvotes: 0

Damith
Damith

Reputation: 63105

If you close the main form main thread will be closed with all the child forms.

What you can do is hide the main form and open new child form.

this.Hide();
CashDeposit cdd = new CashDeposit();
cdd.FormClosed += new FormClosedEventHandler(cdd_FormClosed);
cdd.Owner = this.Owner;
cdd.Show();

Create even for child form close, then you can close the main form at the same time or reopen the main form.

void cdd_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Show(); // or this.Close(); depend on your req.
}

Upvotes: 0

Ekk
Ekk

Reputation: 5715

You can fix your problem by change following code (in your CashDeposit)

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    this.Close();
    CashDeposit cdd = new CashDeposit();
    cdd.Show(this);
}

to

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    this.Close();
    CashDeposit cdd = new CashDeposit();
    cdd.Show(this.Owner);
}

Upvotes: 2

Related Questions