Form.Hide() and Form.Visable = false; Don't Work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Form.Hide() and Form.Visable = false; Don't Work

    This function is in a class file, can't get the frmLogin form to hide when a user logins in, the frmMain will open fine though!???

    Code:
    public void login(string username,string password)
           {
               DBConnect(); //Internal DB connection call
    
               frmLogin login = new frmLogin();
    
               md5 = new MD5CryptoServiceProvider();
               originalBytes = ASCIIEncoding.Default.GetBytes(password);
               encodedBytes = md5.ComputeHash(originalBytes);
    
               QueryString = "SELECT username,password FROM userdat WHERE username='" + username + "'";
               command.CommandText = QueryString;
               try
               {
                   Reader = command.ExecuteReader();
                   Reader.Read();
                   try
                   {
                       if (Reader.GetValue(0).ToString() != null)
                       {
                           if ((username == Reader.GetValue(0).ToString()) && (BitConverter.ToString(encodedBytes) == Reader.GetValue(1).ToString()))
                           {
                               login.Hide();
                               new frmMain().Show();
                           }
                           else
                           {
                               MessageBox.Show("Username or password are incorrect.", "Simplicity", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                           }
                       }
                   }
                   catch(MySqlException)
                   {
                       MessageBox.Show("Username is incorrect.", "Simplicity", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                   }
                    
    		   }
               catch (MySqlException ex01)
               {
                   MessageBox.Show("Error communicating with the database.\nInternal Error Code: x00001\nError Code: "+ex01, "Simplicity", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
    
               DBDisconnect(); //Internal DB close connection call.
           }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You are not hiding your existing login form, you are creating a new one and hiding THAT one.
    You will need to hide the instance of the one already opened.

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      I've added the function to return a boolean value to the login form so if the login details are all correct it'll return a true value so on the login form i have the below if statement and it seems to be working ok, thanks for that Plater.

      Code:
      validate = dbconn.login(txtUsername.Text, txtPassword.Text);
      if (validate)
         this.Hide();

      Comment

      Working...