|
-
Mar 21st, 2004, 01:43 PM
#1
Thread Starter
Lively Member
opening and closing windows forms
I have 2 forms, form1 and form2.
All I wanna do is open form2 from form1 (using a button on form1). Then close form1 so that just form2 is showing.
I also want a button on form2 that will close the application.
The above tasks are very easy in VB6 code - it seems a little tricky in C#.
Anyone help/advise me?
thanks
-
Mar 21st, 2004, 04:45 PM
#2
Addicted Member
Pass reference of form1 in the form2 constructor an hide the form. Here is an example
Parent Form
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
public class oldForm : Form
{
private Button btnOpen;
private newForm child;
public oldForm()
{
this.btnOpen = new Button();
this.SuspendLayout();
this.btnOpen.Location = new Point(80, 96);
this.btnOpen.Name = "btnOpen";
this.btnOpen.Size = new Size(240, 40);
this.btnOpen.TabIndex = 0;
this.btnOpen.Text = "Open New Form";
this.btnOpen.Click += new EventHandler(this.btnOpen_Click);
this.AutoScaleBaseSize = new Size(5, 13);
this.ClientSize = new Size(392, 349);
this.Controls.Add(this.btnOpen);
this.Name = "oldForm";
this.Text = "oldForm";
this.ResumeLayout(false);
}
private void btnOpen_Click(object sender, EventArgs e)
{
child = new newForm(this);
child.Show();
}
[STAThread]
static void Main()
{
Application.Run(new oldForm());
}
}
Child Form
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
public class newForm : Form
{
private Button btnClose;
private oldForm parent;
public newForm(oldForm f)
{
this.btnClose = new Button();
this.SuspendLayout();
this.btnClose.Location = new Point(32, 72);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new Size(232, 40);
this.btnClose.TabIndex = 0;
this.btnClose.Text = "Close Old Form";
this.btnClose.Click += new EventHandler(this.btnClose_Click);
this.AutoScaleBaseSize = new Size(5, 13);
this.ClientSize = new Size(292, 273);
this.Controls.Add(this.btnClose);
this.Closed += new EventHandler(this.newForm_Closed);
this.Name = "newForm";
this.Text = "newForm";
this.ResumeLayout(false);
this.parent = f;
}
private void newForm_Closed(object sender, EventArgs e)
{
// End program
this.parent.Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
// Hide parent do not close, closing will end program
this.parent.Hide();
}
}
-
Mar 21st, 2004, 06:51 PM
#3
PowerPoster
Re: opening and closing windows forms
Originally posted by Claxer
The above tasks are very easy in VB6 code - it seems a little tricky in C#.
This is because .Net is more object oriented.
When you start an windows form application, you are loading and showing a 'start' form. When that form is closed, the application thread dies and hence, the whole application is closed.
As mentioned, you can load form1, do what you need to do, create form2, pass a reference of form one to it, then hide form1 (don't close it). Since form1 is still in memory running and hasn't been closed, the app will continue to run.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|