Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I programmed a c# application and i want when i go from first form to second form, first form be closed.i don't want hide first one i want close it to stop it and go to form2.because when i want copy a file into second form i have exception that says a process is runnig that is from first form because this file used in first form too.

What I have tried:

i tried:
Backup form2 = new Backup();
     form2.ShowDialog();
     this.close();

but it's not my result
Posted
Updated 7-Nov-17 3:19am
v2
Comments
BillWoodruff 7-Nov-17 6:39am    
Is this a winform application ?
Member 11727674 7-Nov-17 6:40am    
Yes it is
Karthik_Mahalingam 7-Nov-17 7:13am    
try
form2.Show();

For closing of form , you can use this.close();
otherwise , once show the popup message saying , do you want to exit , if the user clicks on yes then you can usee the below code
if(DialogSettingsCancel.ShowDialog() == DialogResult.Yes)
{
     this.close();
}


hope this helps
 
Share this answer
 
Comments
Member 11727674 7-Nov-17 6:41am    
When i do it the second form didn't open and application closed.
The problem is that when you close the main form, the application is automatically closed by the system, and there is no way to prevent that - that us the way it is all supposed to work.

What you could do is: edit the program.cs file and change the startup form to your second form - it's the line that says something like this:
Application.Run(new frmMain());
Then in the second form constructor, open the original first form using ShowDialog
When the first form closes, the second form will continue from that point and be displayed.
 
Share this answer
 
Comments
Member 11727674 7-Nov-17 8:36am    
But i don't want second form being my startup project.is it possible?
OriginalGriff 7-Nov-17 8:46am    
Your second form won't show until after the original first form has closed - so it will look like the original first form is the "real first form" to the user.

Give it a try - you'll see what I mean.
First thing is to understand that when you create a WinForm app, and launch it, the Form it launches in the Program.cs 'Main method ... referred to as the 'Main Form' ... when it's closed ... will close the Application, and all other Forms the app has opened.

So, you could do this:

1. have the app launch what is now your second form as its main form.

2. hide the main form window, and show what is now your main form

3. when the secondary form is closed, show the main form:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TwoFormApp
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            // stop layout
            SuspendLayout();
        }

        // what was your Main Form
        private SecondaryForm sForm;

        private void Form1_Load(object sender, EventArgs e)
        {      
            sForm = new SecondaryForm();
            sForm.Closing += SFormOnClosing;

            sForm.Show();
        }

        private void SFormOnClosing(object sender, CancelEventArgs cancelEventArgs)
        {
            // show the main foem
            this.Show();
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            // hide the main form
            this.Hide();
            ResumeLayout();
        }
    }
}
The WinForm App will allow you to hide the main form only when the 'Shown event is triggered during the start-ui process.

Note: I doubt you are going to want to close the second Form shown (now, your main form) without getting/saving.using some data the user entered/selected on that form ... do you know how to do that ?
 
Share this answer
 
Comments
Member 11727674 7-Nov-17 8:35am    
I don't want second form being the startup form of project i want by click on a button in form1, form1 close and form2 start.is it possible?
BillWoodruff 7-Nov-17 9:04am    
The fact is: with a standard WinForm application, closing the Main Form will close the app and all open Forms.

If you want other behavior, then you have to change your app's architecture. There is a way to start a WinForm app with no "Main Form" (using a custom ApplicationContext), and multiple Forms. I doubt that is what you want, here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900