Hi nadim,
According to your second post's requirement. Here is a sample which can meet your need.
Main form
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
FrmLogin frmLogin = new FrmLogin();
if (frmLogin.ShowDialog() == DialogResult.OK)
{
// Login succeeded, do the following work
}
else
{
// Login failed
this.Close();
}
}
}
Login form
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text;
string password = txtPassword.Text;
if (Check(userName, password))
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
this.DialogResult = DialogResult.Cancel;
MessageBox.Show("User name or password is not correct");
this.Close();
}
}
private bool Check(string userName, string password)
{
// Check here. If succeeded return true, else reture false
return true;
}
}
In this sample, I have handled the Load event of FrmMain to show FrmLogin. The FrmLogin will require user name and password and do the check. If user pass the check, you can set the DialogResult to "OK". If user failed to pass the check, you can set the DialogResult to Cancel. The main form will detect whether the user has passed the cheeck based on the FrmLogin's DialogResult.
If you have anything unclear, please feel free to tell me.
Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the
All-In-One Code Framework!