hi sir,
i have a textbox and loginbutton in first form and wen i click on a valid id in 1st textbox wch is in the database and click on loginbutton it should show the details of that id in the next form which consists of textboxes and a modify button i am unable to know how to do dis please help

Create an instance of Second Form's object and request for message Show().

....
Form2 a=new Form2();
a.Show();

Close or Hide your first Form (login)
Show a second Form.
Form idInfo = new Form();
idInfo.Show();

This is actually a bit more tricky then its given credit. The problem is, when you run your first form, that is, in the program.cs file, you call Application.Run(new Form1); //or whatever the log in form is called.

It then becomes responsible for the thread, if you close it. Then the application exits. so if you want to Close the login form when you are done with it. your application will exit. To keep this from happening you have multiple viable solutions. you could run the main form (the one that will accept the login information) then hide it, and have it call and create the login form. then simply pass the data into the main from when the login is closed.

or, you could create the login form manually in the Main() method, then call Application.Run() with no args, and when you close any forms or all the forms, the program still runs, and then just create an event handler for you main form for onclosed and call Application.exit();

Now for something a little more advanced, you could created an applicationContext class, that would create all your forms for you, and run them in the pattern of your choosing, then pass that class to your Application.Run(ApplicationContext); method.

These are just some ways to handle it, there are more, but when it comes down to it. Its which ever best suits your application. But either way, its very important to remember, just because visual studio creates our main() method, doesn't mean you won't have to edit it from time to time.

Best of luck with you application,
and as always, Happy Coding.

Comments
Closing the main form can be tricky, you explained why,thanks!

I have a similar issue. I have modified my call to Application.run in the program.cs file, so that it is not responsible for the thread.

frmLogin login = new frmLogin();
login.Show();
Application.Run();

The issue I have now is after I log in the second form displays, but the login form does not close. Its just hidden behind my main application form. What do i need to do for the login form to close completely?

login form code when the login button is clicked

/// <summary>
        /// The button that is clicked when logging into the program
        /// </summary>
        /// <param name="sender">The login button that received the event</param>
        /// <param name="e">mouse event args</param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string email = txtEmail.Text;
            string password = txtPassword.Text;

            if (email != "" && password != "")
            {
                Employee employee = Employee.GetEmployee(txtEmail.Text, txtPassword.Text, out employee);
                if (employee == null)
                {
                    lblLoginVal.Text = wrongLogInMsg;
                    txtPassword.Text = "";
                }
                else
                {
                    //Open Main CRM Form
                    CRMMain crmMainForm = new CRMMain();
                    crmMainForm.employee = employee;
                    crmMainForm.Show();
                    
                }
            }
            else
            {
                lblLoginVal.Text = emptyLogInMsg;
            }
        }

here is the main method for my second form

public CRMMain()
        {
            InitializeComponent();
            frmLogin login = new frmLogin();
            login.Close();
        }

you need to call the close() method from inside the login form's code...

just add the line to this section

//Open Main CRM Form
                    CRMMain crmMainForm = new CRMMain();
                    crmMainForm.employee = employee;
                    crmMainForm.Show();
                   [B]this.Close();[/B]//this will cause the login form
                   //close  after the main form is opened

otherwise you would need to pass the instance of the login form to the main form in order to reference it to close it. your code actually creates a new form, never shows it, but then closes it.

what is unnecessary, but you could do in that way would be make these changes...

//Open Main CRM Form
                    CRMMain crmMainForm = new CRMMain([B]this[/B]);
                    crmMainForm.employee = employee;
                    crmMainForm.Show();

then in the main form constructor reference that for a close like...

public CRMMain([B]Form lgfrm[/B])
        {
            InitializeComponent();
           [B]lfgrm.Close();[/B] 
        }

best of luck :)

Comments
Helped correct my code

Great that worked! That brings up another issue now. In my Login.cs and my CRMMain.cs file I added the following

private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
 {
            Application.Exit();
 }

This will exit the application when the form is closed, which I do not want to happen unless they click the close icon in the top right on any form. Is there a way to tell when the close icon is clicked vs when i call the close().
thanks

this is only a problem in the login form, because you will need it to be able to exit the application in the event that a user cannot logon, but the form needs to close when you do log in, but there is a simple solution.

Just create a bool variable, that will tell your close event if its being closed because you logged in successfully...

//create a variable with the full class scope.
private bool exitClose = True;
//edit the form closed handler to check for flag
 private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
 {
if(exitClose == True)
{
            Application.Exit();
}

 }

then just set the value to false if the login is successful like so

//Open Main CRM Form
                    CRMMain crmMainForm = new CRMMain();
                    crmMainForm.employee = employee;
                    crmMainForm.Show();
                     exitClose = false; //prevent the app from closing
                   this.Close();

No problem, if you could, mark this thread solved.

I would if I could. I did not start the thread. Gangadhar619 started this one.

Well maybe they will. I find it unfortunate when a solution has been found, and a thread isn't marked solved. Its much easier when browsing for answers to look search for only solved threads.

This article has been dead for over six months. Start a new discussion instead.