Adding a form to a C# program.

  • Thread starter Thread starter Jonny Relentless
  • Start date Start date
J

Jonny Relentless

After adding a form to a C# program, I am unable to use it. I want to
use form1 as a sort of menu, so that when the user clicks a button,
form1 will close and form2 (or form3, etc) will open. What code must I
write to make this happen? I am new to programming, so go easy on me
with the explanation please.
Thank you for any help you can offer.
 
After adding a form to a C# program, I am unable to use it. I want to
use form1 as a sort of menu, so that when the user clicks a button,
form1 will close and form2 (or form3, etc) will open. What code must I
write to make this happen? I am new to programming, so go easy on me
with the explanation please.
Thank you for any help you can offer.

Once you have a form class defined, say, Form2, you can create it
using:

Form2 frm2 = new Form2();

Then you can show it with:

frm2.Show();

If you want to hide your original form, you can do this:

form1.Hide(); // Assuming the original form is called form1.

If you close the main form for you application (the one created in
program.cs) the program will exit, unless you
change that code.

Matt
 
Jonny Relentless said:
After adding a form to a C# program, I am unable to use it. I want to
use form1 as a sort of menu, so that when the user clicks a button,
form1 will close and form2 (or form3, etc) will open. What code must I
write to make this happen? I am new to programming, so go easy on me
with the explanation please.
Thank you for any help you can offer.

Add a Button to Form1. Double-click the button to open its "click" event
handler in the code-behind. Enter the following:

Form2 frm = new Form2();
frm.Show();
this.Close();

The first instruction creates an instance of the form2. The second
instruction shows it on screen (in a non-modal way, so your code continues
executing the next line). The third instruction closes the form that is
currently executing, which is the Form1 that contains the button.

Do the same with additional buttons and additional forms. As you continue to
learn, you will find that there are ways to simplify this, so that a single
"click" handler can be used for multiple buttons, but you don't need to do
this if you are just starting to experiment.
 
Add a Button to Form1. Double-click the button to open its "click" event
handler in the code-behind. Enter the following:

Form2 frm = new Form2();
frm.Show();
this.Close();

The first instruction creates an instance of the form2. The second
instruction shows it on screen (in a non-modal way, so your code continues
executing the next line). The third instruction closes the form that is
currently executing, which is the Form1 that contains the button.

Do the same with additional buttons and additional forms. As you continue to
learn, you will find that there are ways to simplify this, so that a single
"click" handler can be used for multiple buttons, but you don't need to do
this if you are just starting to experiment.

Thanks! That seems to be working, except that as soon as I click the
button, form2 opens and then immediately closes, ending the debugging
session. Do you know why? I added a button to form2 (trying to make it
go back to form1), but it doesn't wait for input from the user, it
just closes.
 
Thanks! That seems to be working, except that as soon as I click the
button, form2 opens and then immediately closes, ending the debugging
session. Do you know why? I added a button to form2 (trying to make it
go back to form1), but it doesn't wait for input from the user, it
just closes.

If you open the program.cs file for your project you will see
something like this:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

The new Form1() terminates the program when the form is closed. Thus,
you can't
"close" form1, but you can hide it.

Matt
 
Once you have a form class defined, say, Form2, you can create it
using:

Form2 frm2 = new Form2();

Then you can show it with:

frm2.Show();

If you want to hide your original form, you can do this:

form1.Hide(); // Assuming the original form is called form1.

If you close the main form for you application (the one created in
program.cs) the program will exit, unless you
change that code.

Matt

Aah, great! That worked. Thank you for your help!
 
[...]
If you close the main form for you application (the one created in
program.cs) the program will exit, unless you
change that code.

That's correct. Application.Run() adds a handler for the passed-in form's
Closed event, in which it calls ExitThread().
http://msdn2.microsoft.com/en-us/library/ms157902(VS.80).aspx

However, hiding the form is only an appropriate solution if you're keeping
a reference to that form and intend to show it again later. If the form
is intended to remain closed, then you really should be closing it
(allowing it to be disposed).

In this case, yes the Main() method in the Program class will need to be
modified, so that it just calls Run() without passing a form (showing the
initial form first, of course), and you'll need to add an explicit call to
Application.ExitThread() where you really want the application to be shut
down, but that's much better than being sloppy about resource management..

After you're done, it'll look something like this (leaving out the default
stuff):

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Form1().Show();
Application.Run();
}
}

public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();

form2.Show();
Close();
}
}

public partial class Form2 : Form
{
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.ExitThread();
}
}

Don't be afraid to edit Program.cs to suit your needs. It's generated by
the project template, but it's not part of a designer or anything. Once
it's in your project, you can always modify it as appropriate.

Pete
 
Jonny Relentless said:
Thanks! That seems to be working, except that as soon as I click the
button, form2 opens and then immediately closes, ending the debugging
session. Do you know why?

I suspect that Form1 is the first form that you open in your program, and
you are opening it the "default" way, which is
Application.Run(new Form1());
(this code is automatically generated for you inside Program.cs).

When you start the form like that, the application terminates when you
close Form1. Since you are closing it after showing Form2, that is why Form2
is closing.
There are two possible solutions:
(1) Hide Form1 instead of Closing it (just change this.Close() into
this.Hide()).
or (2) Modify Program.cs like this:
(new Form1()).Show();
Applicaton.Run();
 

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

Back
Top