Hello, i have some application (windows form) the first window (Main) is a Mdi. i need when the client open the application the main windows will open and will open new win for username and password but the client need to be Unavilable to minimize or close the login win and not be able to use the menu above in the main Mdi win until he will fill correct username and password. i know how to show the login win and to handle incorrect user/pass but how can i block the main win behaind until the user put correct pass and user?

Recommended Answers

All 2 Replies

Show the login form first, then if the login is successful, open the MDI form from the login form:

void buttonLogin_Click(object sender, EventArgs e)
{
    string user = textBoxUser.Text;
    string pass = textBoxPass.Text;

    if (IsValid(user, pass))
    {
        new MainForm().Show();
        Hide();
    }
    else
    {
        MessageBox.Show("Invalid user name or password");
    }
}

But do not forget that the login form is still the controlling form for the application. If you exit the main form, it will not close the application because the main for is not the controlling form. In the main form, add a handler for the FormClosed event that will close the application:

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

A proper way to handle this situation:

private void MainForm_Load(object sender, EventArgs e)
        {
            frmLogin a = new frmLogin();
            DialogResult result=a.ShowDialog();
            if (result == DialogResult.Cancel)
                Application.Exit();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.