Beginner: How do I make form visible again?

D

Dave

I have a button on Form1 that hides the form and displays Form2:

Form2 myForm2 = new Form2();
myForm2.Show();
this.Hide();

After I do some work in Form2 I want to close it and redisplay Form1. I can
close the form with:
this.Close();

But how to I invoke the Show method of Form1? IOW how do I make Form1
visible again?

What I want to call while closing Form2 is "Form1.Show(); " but this throws
an object reference error.

I don't need to instantiate a new Form1. I just need to reference the one
that already exists and make it visble.

Arrrgh!!!! Can someone provide some insight?
 
J

John M Deal

For this I'd setup Form2 with a constructor that takes a generic form
parameter, then in the closing event of Form2 I'd set the form that was
passed in as part of the constructor to be visible. This will allow you
to use any form as the parent of Form2 (in case it happens to be
reusable elsewhere). Hope this helps.

Have A Better One!

John M Deal, MCP
Necessity Software
 
G

Guest

Hi
There is also another way

Create our custom event in Form2 ;
Make our Form1 Subscribe for this event ; (pass the method which has the
code this.show() when subscribing )
In the closing event of Form2 call this custom Event


hope this works
regards
Ansil
Technopark Trivandrum
 
D

Dave

John

But then don't I have two versions of form1 in memory?

If I understand correctly, this is what I have:

On Form1:

private void button1_Click(object sender, System.EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
this.Hide();
}

On Form2:

private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Form1 myForm1 = new Form1();
myForm1.Show();
}

This displays a Form1 when Form2 closes but it is a new Form1. The original
Form1 remains hidden so now I have 2 Form1s in memory.

Is this correct?
 
D

Dave

Thanks Ansil

I will give this a try.



Ansil MCAD said:
Hi
There is also another way

Create our custom event in Form2 ;
Make our Form1 Subscribe for this event ; (pass the method which has the
code this.show() when subscribing )
In the closing event of Form2 call this custom Event


hope this works
regards
Ansil
Technopark Trivandrum
 
K

Kevin Yu [MSFT]

Hi Dave,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to show the caller form when
Form2 is closed. If there is any misunderstanding, please feel free to let
me know.

Here I think John means to pass the reference to the first form to Form2
when it is initialized. So we have to write another constructor for Form2.
The following is the Form2 code.

private System.Windows.Forms.Form fCaller;

public Form2(Form f)
{
this.fCaller = f;
this.InitializeComponent();
}

private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
this.fCaller.Show();
}

Here is the code on Form1.

private void button1_Click(object sender, System.EventArgs e)
{
Form2 myForm2 = new Form2(this);
myForm2.Show();
this.Hide();
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
D

Dave

Thanks Kevin, that was a very helpful explanation.

BTW are te following 2 statements identical? They both appear to accomplish
the same thing.

this.fCaller.Show();

or

this.fCaller.Visible=true;
 
K

Kevin Yu [MSFT]

Hi Dave,

Yes, they accomplish the same thing. According to MSDN document, "Showing
the control is equivalent to setting the Visible property to true. After
the Show method is called, the Visible property returns a value of true
until the Hide method is called."

You can check the following link for more information

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscontrolclassshowtopic.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top