473,399 Members | 3,919 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,399 software developers and data experts.

Show new form and close current form

Hi all,

I need help. I've 3 forms, mainform, form1 and form2. In my mainform, I've 2 buttons; button1 and button2, which I used to call form1 and form2. But I want to close the mainform and show the new is called, and back to mainform when I press back button in the Dat form. Here is my code

private void bnt1_Click(object sender, EventArgs e)
{
Dat dat = new Dat();
dat.FormClosed += new
FormClosedEventHandler(this .dat_FormClosed);
dataLogger.Show();
this.Hide();
}
void dat_FormClosed(object sender, FormClosedEventArgs e)
{
//this.Show();
this.Close();
}
Please help.
abekubuz
Oct 18 '11 #1
1 2171
GaryTexmo
1,501 Expert 1GB
It's important to note here that your MainForm is likely your application host. Have a look in your Program.cs file and see the code that's written there. This is autogenerated by Visual Studio 2005 and up and this file hosts your application's entry point. There's typically a line in there that reads as such...

Expand|Select|Wrap|Line Numbers
  1. Application.Run(new MainForm());
So this means when MainForm closes, your application closes. To get around this, you have two options...

1) Your MainForm is always "alive". You never close it (call the Close method) but instead hide it with the Hide method. You can then use the Show method to restore it at a later time. Your child forms will need a way to tell the parent form to restore itself and you have many options for this and should check out Curtis Rutland's insight on the topic: http://bytes.com/topic/c-sharp/insig...ny-other-forms. Use this if it's ok for your MainForm object to persist.

2) Instead of MainForm driving your application, have another class/form handle it that's more transparent to the user. This acts as a host that will essentially direct traffic, showing the form that's appropriate to be visible. This will let you close all your forms and re-open them and allow your application to continue running. Use this if you absolutely need to create/destroy your MainForm object.

I hope that helps. Please feel free to post back if you have more questions :)
Oct 18 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dan | last post by:
Hi. I'm trying to launch a second form from code in a first, but it doesn't seem to working. Originally I tried to write the code as follows (Form2 has a public method called AttachDataSet): ...
4
by: Claudia Fong | last post by:
Hi, I have more than one forms in my program, and I would like to for example, in form1, I will call form2 and in form2 it will call form3. But the problem is when I call form2.Show, it does...
3
by: Kimelia | last post by:
I want to create something like wizard, which will ask one question and proceed to the next question. So, it's like showing Form A (Question 1), then when the user press "Next" button, I want to...
5
by: prob | last post by:
I would like to acheive the following VB6 code in VB.NET Code in the Form1 - Button1_Click Dim obj1 as new form2 obj1.Prop = myprop obj1.show unload me
4
by: DraguVaso | last post by:
Hi, I want a Login-form that, once the username and password are validated, opens a new Form (of the 'main'-application), and closes itself. I tryed it like this: putting the following code in...
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...
2
by: vooose | last post by:
If you want to shut a window down, which would you use? If I call Dispose() the Closing events arent called (which is what I'm intending) but is there any problems with this? Regards --...
4
by: ghadley_00 | last post by:
Hi, Can anyone recommend a piece of VBA code I could attach to a button in a MS Form form that will close the current form and bring the switchboard to foreground. I'm trying to integrate a form...
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...
9
Microblitz
by: Microblitz | last post by:
Two weeks (Well about 8 hours actually) into writing VBA code and I've run up against a problem. This is the senario; ---- I have a "Main" form in which an entry on a drop down selects one of...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...
0
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...

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.