473,473 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding a form to a C# program.

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.
Feb 27 '08 #1
4 1859
"Jonny Relentless" <Jo*************@gmail.comwrote in message
news:7d**********************************@x30g2000 hsd.googlegroups.com...
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.

Feb 27 '08 #2
On Feb 27, 12:25*pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"Jonny Relentless" <JonnyRelentl...@gmail.comwrote in message

news:7d**********************************@x30g2000 hsd.googlegroups.com...
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.
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.
Feb 27 '08 #3
On Feb 27, 12:21*pm, Jonny Relentless <JonnyRelentl...@gmail.com>
wrote:
On Feb 27, 12:25*pm, "Alberto Poblacion" <earthling-

quitaestoparacontes...@poblacion.orgwrote:
"Jonny Relentless" <JonnyRelentl...@gmail.comwrote in message
news:7d**********************************@x30g2000 hsd.googlegroups.com...
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.

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(fals e);
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

Feb 27 '08 #4
"Jonny Relentless" <Jo*************@gmail.comwrote in message
news:1b**********************************@c33g2000 hsd.googlegroups.com...
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();
Feb 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: filip stas | last post by:
How do i add references during runtime?
16
by: Picho | last post by:
Hi all, Is there any .NET way (I am not rulling out API usage) to add button(s) to a form's title bar? I found some non-.NET solutions that did actually work in VB6 but not in the ..NET...
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
2
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
0
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta...
0
by: jkendrick75 | last post by:
I am new to vb.net and am using visual studio.net 2003. My problem is coming from a program that i am trying to write. I initially added a new form to the program and changed the project property...
9
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
4
by: ifitzgerald | last post by:
Hi, I am modifying a rather large and complex MFC application (written by someone else) written in Visual C++ 6.0 with service pack 6. I need to add serial communication functionality to the...
3
by: raylopez99 | last post by:
Oh, I know, I should have provided complete code in console mode form. But for the rest of you (sorry Jon, just kidding) I have an example of why, once again, you must pick the correct entry point...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.