473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# form close problem

Hi,

In my C# windows form project, how do I in a formA open another formB, and
close formA when formB is opened?
Thanks for help.
Jason
Aug 18 '06 #1
4 25555
Hi Jason,

You are probably better off having formA and formB share a common parent
responsible for the message loop.
If formA is started from Main, have Main start both forms. Pass
properties to formB via Main.

What are you trying to do?
On Fri, 18 Aug 2006 07:28:36 +0200, Jason Huang
<Ja************@hotmail.comwrote:
Hi,

In my C# windows form project, how do I in a formA open another formB,
and
close formA when formB is opened?
Thanks for help.
Jason



--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 18 '06 #2
You can do so using Show and Close method of the Form. Let us suppose that
you have 3 forms, Form1 (the main form), Form2 and Form3.

From Form2, if you want to open Form3 and close Form2, you can use the
following code:

Form3 f3 = new Form3();
f3.Show(); // Opens the Form3

this.Close(); // Closes the current form (Form2)

If you close Form1 (which might be the main form), the entire application
will close.

--
Nand Kishore Gupta
"Jason Huang" wrote:
Hi,

In my C# windows form project, how do I in a formA open another formB, and
close formA when formB is opened?
Thanks for help.
Jason
Aug 18 '06 #3
On Fri, 18 Aug 2006 08:14:01 +0200, Nand Kishore Gupta
<Na**************@discussions.microsoft.comwrote :
You can do so using Show and Close method of the Form. Let us suppose
that
you have 3 forms, Form1 (the main form), Form2 and Form3.

From Form2, if you want to open Form3 and close Form2, you can use the
following code:

Form3 f3 = new Form3();
f3.Show(); // Opens the Form3

this.Close(); // Closes the current form (Form2)

If you close Form1 (which might be the main form), the entire application
will close.
Not if you fire up another Form in your Main method

Application.Run(new Form1());
Application.Run(new Form2()); // will start when Form1 ends

--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 18 '06 #4
Not sure you want to create two forms in one application that way, I'm pretty
sure you end up on two different threads.

You can also start the application without tying it to a particular form

Form1 frm1 = new Form1();
frm1.Show();
Application.Run();

Then you can open and or close forms without the problem of shutting down
the application when you close a form, or having to leave a form in memory
while your app is running even if you don't need it.

Of course it DOES introduce the problem of your having to specifically end
your application.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"Morten Wennevik" wrote:
On Fri, 18 Aug 2006 08:14:01 +0200, Nand Kishore Gupta
<Na**************@discussions.microsoft.comwrote :
You can do so using Show and Close method of the Form. Let us suppose
that
you have 3 forms, Form1 (the main form), Form2 and Form3.

From Form2, if you want to open Form3 and close Form2, you can use the
following code:

Form3 f3 = new Form3();
f3.Show(); // Opens the Form3

this.Close(); // Closes the current form (Form2)

If you close Form1 (which might be the main form), the entire application
will close.


Not if you fire up another Form in your Main method

Application.Run(new Form1());
Application.Run(new Form2()); // will start when Form1 ends

--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 18 '06 #5

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

Similar topics

1
by: Jules Winfield | last post by:
My WinForms application can have any number of top level forms open at a given time. If the user selects File|Exit, all of the forms are closed. The loop to close the forms looks something like this:...
1
by: Martin Douglas | last post by:
Hey guys, maybe someone can help me with some MDI issues I have. A co-worker asked me a very simple question, one that I blew off as trivial, and it has become a time-consuming issue. Simply...
13
by: Rob Agar | last post by:
hi all, I have a problem which I believe others have experienced before, but as far as I can see has not been resolved. Basically, the form refuses to close. Neither clicking the 'X' button...
2
by: Chien Lau | last post by:
Hi, At a certain point in my application, I have access to a Form object. I want to close that form: form.Close(); There are any number of reasons why this form might not close. A common...
6
by: Jeremy | last post by:
I've spent many hours on this issue, so someone please either tell me I'm an idiot, or give me a hint on where to look. My mdi child has a shared execute method which creates and shows it. When I...
3
by: Sameh Ahmed | last post by:
Hello there IS there a way to cancel a form close after the user clicks on the close button? I want to confirm from the user the close operation and if he chooses not to, I would cancel the...
1
by: sennopati | last post by:
Dear, I have looping statement like this: For Each frmChild As Form In Me.MdiChildren 'Ada validasi untuk periksa status data(sudah disimpan atau belum) frmChild.Close() frmChild = Nothing...
6
by: dotnetnovice | last post by:
Hi everybody. I need some help in c# forms problem which is i have three forms naming main form,form1 & form2. main form is the mdiparent of both form1 and form2. i also have a toolstrip in my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...

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.