Show a Hidden Form

G

Guest

In C# I have a Windows Application and a Startup form created by
static void Main()

Application.Run(new Form1())


On Form1 I have a button which creates a new instance of a Form2 and Shows Form2. The same onclick event Hides Form1
On Form2 I have a Button, which when clicked, I want Form2 to Close and Form1 to Show again. But I cannot find a way to reference Form1 from Form2 in order to show it. Can anyone help please?
 
G

Guest

Thanks

But I get the following error

Cannot implicitly convert type 'System.EventHandler' to 'System.ComponentModel.CancelEventHandler'

This is the code for my Button Click on Form1

private void button1_Click(object sender, System.EventArgs e

Form2 myForm = new Form2()
myForm.Closing += new EventHandler(procedure_name)
myForm.Show()
this.Hide()


private void procedure_name(object sender, EventArgs e

this.Show()


I am new to C#. Where am I going wrong please?
 
H

Heinz

make another Constructor in Form 2 with Form1 as a parameter

Form1 form1;

public Form2 (Form1 form1)
{
//here verything else you need
this.form1 = form1;
}

than you can use the form1 object to hide the Form1.
Hope this helps

Geoff Murley said:
In C# I have a Windows Application and a Startup form created by:
static void Main()
{
Application.Run(new Form1());
}

On Form1 I have a button which creates a new instance of a Form2 and Shows
Form2. The same onclick event Hides Form1.
On Form2 I have a Button, which when clicked, I want Form2 to Close and
Form1 to Show again. But I cannot find a way to reference Form1 from Form2
in order to show it. Can anyone help please?
 
B

Bob Powell [MVP]

The Closing event has an event handler and arguments that enable you to
cancel the close. For example, when the user decides to cancel rather than
save the file.

The delegate for this event is the CancelEventHandler which takes a
CancelEventArgs.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41
 
H

Heinz

When a form is showing and I want that it is no more showing I use
form.visible = false and when i need it again i set visible = true
what is the difference to
form.Hide();

??
 
A

Alok

in form1 trap the closing event of form2.
show form1 in this..

Code:

form2.Closing += new eventhandler(procedure name);
form2.Show();

private void procedure_name(object sender, eventargs e)
{
this.show();
}

Geoff Murley said:
In C# I have a Windows Application and a Startup form created by:
static void Main()
{
Application.Run(new Form1());
}

On Form1 I have a button which creates a new instance of a Form2 and Shows
Form2. The same onclick event Hides Form1.
On Form2 I have a Button, which when clicked, I want Form2 to Close and
Form1 to Show again. But I cannot find a way to reference Form1 from Form2
in order to show it. Can anyone help please?
 
G

Guest

Thanks very much everyone. I have now got it to work with

protected void Form2_Cancel (Object sender, CancelEventArgs e)

this.Show()


private void button1_Click(object sender, System.EventArgs e

this.Hide()
Form2 frm2 = new Form2()
frm2.Closing += new CancelEventHandler(this.Form2_Cancel)
frm2.Show()
}
 

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