473,614 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ 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 1868
"Jonny Relentless" <Jo************ *@gmail.comwrot e in message
news:7d******** *************** ***********@x30 g2000hsd.google groups.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-
quitaestoparaco ntes...@poblaci on.orgwrote:
"Jonny Relentless" <JonnyRelentl.. .@gmail.comwrot e in message

news:7d******** *************** ***********@x30 g2000hsd.google groups.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-

quitaestoparaco ntes...@poblaci on.orgwrote:
"Jonny Relentless" <JonnyRelentl.. .@gmail.comwrot e in message
news:7d******** *************** ***********@x30 g2000hsd.google groups.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.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (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

Feb 27 '08 #4
"Jonny Relentless" <Jo************ *@gmail.comwrot e in message
news:1b******** *************** ***********@c33 g2000hsd.google groups.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
6578
by: filip stas | last post by:
How do i add references during runtime?
16
3181
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 forms... I tried painting, but the paintaing area provided by the form is only the client area - no visible way to paint on the title bar.
5
5915
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 of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should...
2
3718
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 the Controls.Add() method several times or even calling the Controls.AddRange() method once can take a huge amount of time. Therefore, I would like to be able to run the loop that creates the controls on a different thread and meanwhile give the user...
0
2394
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 version, the program is not working as in version 1.1. So what is the problem? Microsoft declared that the version 2.0 is fully backward support, but it is not. Is that the problem with 2.0 version? I find out the problem in version 2.0. After...
0
918
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 to make the new form the starting form. when i press F5 to start the program, it still goes to my old form and does not show my new form. Since then, as i wasn't too far into the program, i decided to rewrite the program, creating the form that...
9
4475
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 Controls.Add(list), where list is my ListView control, then program hangs for minute. It seems like this time depends on number of records added to control. So probably when adding control to my form, those records are actualy added at this time...
4
5514
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 program, and since I am very familiar with the Microsoft Comm Control from Visual Basic I decided that I would use that in this application. To make things easier on myself, I decided to make several test applications using the MS Comm control in VC++...
3
1632
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 in your code when adding a class (oops, I meant variable, or instantiation of a object that's a class) to a form constructor. Specifically, adding a class variable should always come BEFORE the statement "InitializeComponent()" in your form...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8279
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8433
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7093
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.