2
votes

I created a two forms loginform and mainform the login form is my startup program which validate if the username and password is correct then it will open the main form but the problem is after the main form is open the login form is still at the back of main form.

In my btnSubmit_Click in my loginform I tried different command this.Hide(), this.Close(), this.Disposed(), also in mainform on load I also execute these commands to close the loginform but no luck still it is there, I cannot move it or close it manually.

loginform is my startup program.

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLog());
        }

Please help me with this problem.

4
frmlog is loginform sorry for renaming it I just wan't to make it clearer.user2262382
I am using C# windows form vissual studio 2010 express edition.user2262382
Try this answer: C# Opening a new form and closing the other one.Jacob Seleznev
can you share the code where you are showing the mainform after validationArshad
You could start your application with the mainform and then immidiately the loginform as a dialog. If login fails you can close your mainform.Alina B.

4 Answers

1
votes

a simple solution would be to set your login form to variable and set public properties with the login result and then do another Application.Run on your main form just below your Application.Run on your login form. you would either have to call your close method on your login form or set the dialogresult property to close your login form and move to the main form.

1
votes

if you're gonna use login form again, then u should only hide login form.

mainform mf = new mainform();
mf.Tag = this;
mf.Show();
this.Hide();

and to show login form again

var log = this.Tag as frmLog;
log.Show();
1
votes

simply create a new object after validating user credential and hide previous one, as bewlo :

private void btnLogin_Click(object sender, EventArgs e)
{
    //after successfull validation 
    mainform objMainform = new mainform();
    objMainform.Show();
    this.Hide();
}
1
votes

i think hide isn't good command for this kind of work.. try this..

this is for PROGRAM.CS

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            FrmMain mainForm = new FrmMain();
            FrmUsername us = new FrmUsername();
            if (us.ShowDialog() != DialogResult.OK)
                return;
            Application.Run(mainForm);
        }

and this for enter button in username form..

private void btm_Enter_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("select count(*) from Tbl_Username where Username='" + txt_Username.Text + "' and Password='" + txt_Password.Text + "'", _sqlcon);
            _sqlcon.Open();
            int count = 0;
            count = (int)cmd.ExecuteScalar();
            if (count > 0)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.None;
                MessageBox.Show("UserName or password is wrong", "ENTER", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            _sqlcon.Close();
        }