Click here to Skip to main content
15,849,197 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here i have two forms

LoGEnq form
Enquiry form

and LoGEnq has a message box and i want when i hit ok button close the current form and open a new form.

C#
private void Btn1_Click(object sender, EventArgs e)
        {
            
            SqlConnection cn = new SqlConnection(global::EnQApp.Properties.Settings.Default.Database1ConnectionString);

            try {
                cn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Login", cn))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string name = reader.GetString(0);  // Name string
                        string pass = reader.GetString(1); // Password string
                    }
                    MessageBox.Show("Password Accepted");
                    this.Close();
                }
            }
            Enquiry enq = new Enquiry(UName.Text);
            enq.Show();
        }


but this will close all forms in the application

i just want to close the LoGEnq application when i click message box OK
and open Enquiry Form?
Posted
Comments
anushka Dilshan 1-Dec-14 5:33am    
I assume that this form is the main form or the first form that you have declared in the application.cs file if so do not close this form because it will close the app just hide the form (this.hide())

if you want to close the main (this) form you have to close it after you have opened a new form



Enquiry enq = new Enquiry(UName.Text);
enq.Show();
this.Close();
Er. Dinesh Sharma 1-Dec-14 5:55am    
you have to hide the LoGEnq because its main form of your application
BillWoodruff 1-Dec-14 7:00am    
This appears to me to be the same question you posted a few hours ago, and which you accepted an answer to. If you'll read my response on that first question, you'll also see that I offered to show you a better way to solve this problem.

"I do not enlighten those who are not eager to learn, nor arouse those who are not anxious to give an explanation themselves. If I have presented one corner of the square and they cannot come back to me with the other three, I should not go over the points again.

Confucius"

This is obious that when you will call this.Close() it will close the Form1 and execution will return to Program.cs Main(). There can be a solution. Change your Program.cs
C#
namespace TestWindowApp
{
    static class Program
    {
        public static bool SetForm2 = false;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            if (SetForm2)
            {
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
        }
    }
}


And set SetForm2=true wherever you want to close the current form and want to open Form2(Enquiry).

C#
Program.SetForm2 = true;
this.Close();
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 1-Dec-14 9:01am    
Please accept the solution and rate if it is solving your problem.
BillWoodruff 1-Dec-14 19:59pm    
+5 You are on the right track here.
Praveen Kumar Upadhyay 4-Dec-14 8:10am    
Thank you Bill
Praveen Kumar Upadhyay 1-Dec-14 23:02pm    
Thank you very much. Please accept the solution if it is solving your problem.
Hemas Ilearn 4-Dec-14 4:35am    
But this one won't open my Enquiry form. it will open a new form?
Try this one

C#
if (DialogResult.OK == MessageBox.Show("Password Accepted", "Password", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{

 Form2 frm = new Form2();
 frm.Show();
 this.Close(); // if you are trying to do this in your main form.Use this.Hide();
}
 
Share this answer
 
v2
Comments
TheRealSteveJudge 1-Dec-14 5:45am    
This will not work.
If you do this, Form1 will disappear as well.
Your application's message pump will stop working
because you declared in Program.cs:
Application.Run(new Form1());
Dilan Shaminda 1-Dec-14 5:48am    
yes,that is true.that is why i commented "if you are trying to do this in your main form.Use this.Hide();" :-)
TheRealSteveJudge 1-Dec-14 6:14am    
I tried it.
Form1 disappears, Form2 is shown.
Actually Form1 is hidden and not closed.
Dilan Shaminda 1-Dec-14 6:24am    
yes,if you close the main form whole application will be closed :-)
TheRealSteveJudge 1-Dec-14 7:05am    
My vote of 4 for not being the perfect solution but a good hack.
Enquiry enq = new Enquiry(UName.Text);
    enq.Show();
    this.Close();



Close the form after you open the new form
 
Share this answer
 
Comments
TheRealSteveJudge 1-Dec-14 5:48am    
This will not work.
If you do this, Form1 will disappear as well.
Your application's message pump will stop working
because you declared in Program.cs:
Application.Run(new Form1());

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