1

I have 3 classes: Form1, LoginForm and program.

Program holds my main method that then runs loginform, if a condition inside login form is met then form1 is run.

What I want it to do is hide loginform just before I show form1.

How do I do this since I can't use loginform.hide();

Here's code:

namespace RepSalesNetAnalysis
{
public partial class LoginForm : Form
{
    public  bool letsGO = false;
    public LoginForm()
    {
        InitializeComponent();
    }

    private static DataTable LookupUser(string Username)
    {
        const string connStr = "Server=10asaf;" +
                            "Database=dfafa;" +
                            "uid=bufaf;" +
                            "pwd=dridfsdf;" +
                            "Connect Timdf0;";

        //"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";

        const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName";
        DataTable result = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    result.Load(dr);
                }
            }
        }
        return result;
    }

    private void buttonLogin_Click(object sender, EventArgs e)
    {

        if (string.IsNullOrEmpty(textUser.Text))
        {
            //Focus box before showing a message
            textUser.Focus();
            MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
            textUser.Focus();
            return;
        }
        else if (string.IsNullOrEmpty(textPass.Text))
        {
            textPass.Focus();
            MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            textPass.Focus();
            return;
        }

        //OK they enter a user and pass, lets see if they can authenticate
        using (DataTable dt = LookupUser(textUser.Text))
        {
            if (dt.Rows.Count == 0)
            {
                textUser.Focus();
                MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textUser.Focus();
                return;
            }
            else
            {
                string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
                string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB
                //MessageBox.Show
                Console.WriteLine(string.Compare(dbPassword, appPassword));

                if (string.Compare(dbPassword, appPassword) == 0)
                {
                     DialogResult = DialogResult.OK;
                     this.Close();
                }
                else
                {
                    //You may want to use the same error message so they can't tell which field they got wrong
                    textPass.Focus();
                    MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textPass.Focus();
                    return;
                }
            }
        }

    }
}

}

Am i missing something? heres my main class;

namespace RepSalesNetAnalysis
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        LoginForm fLogin = new LoginForm();
        if (fLogin.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Form1());
        }
        else
        {
            Application.Exit();
        }
    }
}
}
lemunk
  • 2,616
  • 12
  • 57
  • 87

3 Answers3

4

Steven this is all wrong ground up.

there are other ways to do what you need properly having the Main method of your Program class creating a login form and only if login is successful you instantiate and show the main application form.

check this question/answer for details and examples: How can I close a login form and show the main form without my application closing?

you in fact need this approach:

static void Main()
{
    LoginForm fLogin = new LoginForm();
    if (fLogin.ShowDialog() == DialogResult.OK)
    {
        Application.Run(new MainForm());
    }
    else
    {
        Application.Exit();
    }
}
Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • ok so i changed the main class program to your example, when i hit the button on login form nothing happens, because in the if statement im not actually doing anything. what needs to go in there? – lemunk Dec 13 '11 at 14:02
  • if login is successful close form by setting DialogResult to DialogResult.Ok, in the login form. – Davide Piras Dec 13 '11 at 14:04
  • not sure i understand that. in my loginform class, in the if statment where login is successfull, i need to say dialogresult.ok? – lemunk Dec 13 '11 at 14:05
  • ok ive updated my post to show the changes. works fine. is this a hotfix? or is this the way you would do it? i.e better OOP implementation. reason i ask is because im very weak skilled when it comes to OOP – lemunk Dec 13 '11 at 14:12
0
static class Program
{
    private static bool canLogin;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        using (LoginForm loginForm = new LoginForm()){
           // show as dialog
           // perform logic to check if successful
           canLogin = SomeStaticClass.VerifyCredentials(loginForm.Credentials);
           // grab any properties you may want here
        }
        //then run the application
        if(canLogin){
           Application.Run(new Form1());
        }
    }
}
}
IAbstract
  • 19,551
  • 15
  • 98
  • 146
0

You can set DialogResult to OK or Cancel in your LoginForm. This will close your LoginForm and returns the DialogResult back to your Main method. Then check the result in your Main method like this.

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    LoginForm form = new LoginForm();
    if (form.ShowDialog() == DialogResult.OK)
        Application.Run(new Form1());
}

hope this helps

dknaack
  • 60,192
  • 27
  • 155
  • 202