473,809 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Show new form and close current form

1 New Member
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(obje ct sender, EventArgs e)
{
Dat dat = new Dat();
dat.FormClosed += new
FormClosedEvent Handler(this .dat_FormClosed );
dataLogger.Show ();
this.Hide();
}
void dat_FormClosed( object sender, FormClosedEvent Args e)
{
//this.Show();
this.Close();
}
Please help.
abekubuz
Oct 18 '11 #1
1 2184
GaryTexmo
1,501 Recognized Expert Top Contributor
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
15116
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): Application.Run(new Form2()); Form2.AttachDataSet(ds); Form2.Show();
4
21548
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 show the form2, but how can I unload form1? If I put form1.close, it will close everything. :(
3
19234
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 close Form A (Question 1), and show Form B (Question 2). Sorry to ask if this is too simple, as I am still learning C#. Thanks and have a nice day.
5
13549
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
4457
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 the click-event of the login-button of my Login-form: Dim frmAG As New frmAgent frmAG.Show() Me.Close()
6
1829
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 close the form, it visibly goes away, but it remains in memory. I know this because ComboBox events fire for every supposedly "closed" instance of frmMyMDIchild. Obviously there is either a nasty bug in VB, or I completely misunderstand...
2
10285
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 -- Wal http://www.vooose.com
4
9581
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 of buttons into the way the switchboard gets used. Best wishes, George Hadley
6
3696
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 main form which includes option close current frm and close all. what i need is when i click the close current form option the form opened state(either form1 or form2) close down. suppose currently form 2 is currently open in the main form and i...
9
14224
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 three types of test. The type of test designates the type of form used to enter the data. Therefore when my drop down select type 2 from its field or if the user uses the navigation to move through the records, the system must look at the drop...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
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
10376
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...
0
10120
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
9200
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...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4332
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 we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.