What is the problem in my code? C # winforms

I have two forms: Login and Main form. First, the login form will be displayed, and when the user is authenticated, the main form will be displayed and the login form will be closed.

This works, but I have to double-click btnLogin (the button in the login form) to close the login form and show the main form.

Here is my code.

Program.cs (login form)

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

            Login fLogin = new Login();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Main());
            }
        }
    }
}

Login form

namespace Login
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // initially btnLogin has a DialogResult property set to None
            Authenticate();
        }

        private void Authenticate()
        {
            SqlCeConnection conn = new SqlCeConnection(@"Data source=d:/BIMS.sdf");
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand(Properties.Resources.CheckIfUserExists, conn);
            cmd.Parameters.Add("username", txtUsername.Text.Trim());
            cmd.Parameters.Add("password", txtPassword.Text.Trim());

            SqlCeDataReader dr = cmd.ExecuteReader();
            bool hasRow = dr.Read();
            if (hasRow)
            {
                btnLogin.DialogResult = DialogResult.OK;
            }
        }
    }
}

What do you think I'm doing it wrong? Thank....

+3
source share
5 answers

just change

if (hasRow)
{
  // btnLogin.DialogResult = DialogResult.OK;
     this.DialogResult = DialogResult.OK;
     this.close();
}
+3
source

You should just call Form.Close()buttons in the event handler when you want to close the form (after setting what you want DialogResult).

, .

-, .

+1

: -

namespace Login

{

{        ///        /// .       ///

   [STAThread]
   static void Main()
   {
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       Application.Run(new Login());
   }

}

}

< →

namespace Login {     :    {        public Login()         {             InitializeComponent();         }

    private void Login_Load(object sender, EventArgs e)
    {

    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        // initially btnLogin has a DialogResult property set to None
        Authenticate();
    }

    private void Authenticate()
    {
        SqlCeConnection conn = new SqlCeConnection(@"Data source=d:/BIMS.sdf");
        conn.Open();
        SqlCeCommand cmd = new SqlCeCommand(Properties.Resources.CheckIfUserExists, conn);
        cmd.Parameters.Add("username", txtUsername.Text.Trim());
        cmd.Parameters.Add("password", txtPassword.Text.Trim());

        SqlCeDataReader dr = cmd.ExecuteReader();
        bool hasRow = dr.Read();
        if (hasRow)
        {
            Main formmain = new Main();
            formmain.Show();
            this.Dispose(); // U can also use this.Close();
        }
    }
}

}

+1

, Authenticate().

private void Authenticate()
{
   btnLogin.DialogResult = DialogResult.OK;
}

, gui.

, , ( , ..)

0

, Authenticate... , , db.. , Authenticate...

private void btnLogin_Click(object sender, EventArgs e)
{
      btnLogin.Enabled = false; 

      // initially btnLogin has a DialogResult property set to None
      Authenticate();
      // better place call to Authenticate in try catch blocks
      // to prevent btnLogin in a disabled state forever if Authenticate fails with
      // an exception ...
      // also if an exception occurs show that in a message box

      btnLogin.Enabled = true;
}

Main, ,

 Login fLogin = new Login();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                fLogin.Close();
                Application.Run(new Main());
            }
0
source

Source: https://habr.com/ru/post/1787092/


All Articles