层次结构: Mainparent(mdi表单) - EMP_loginpage(子表单)
我在下面提供的代码是在EMP_loginpage表单上的提交按钮上编写的。我希望每当用户以 管理员 的身份登录时,另一个表单应该作为Mainparent的子表单而不是EMP_loginpage打开。
CODE :
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("User ID or Password Cannot Be Blank...!!", "Stop First Enter UserID and Password", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
textBox1.Focus();
}
else
{
if (count == 0)
{
try
{
con.Open();
cmd = new SqlCommand("select * from tbllogin where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr["Rights"].ToString()=="1")
{
uid = dr["Username"].ToString();
name = dr["LoginName"].ToString();
role = "Administrator";
this.Hide();
Homeadmin homeadmin = new Homeadmin(); // Homeadmin is the form i want to get opened under mdi(parent)
homeadmin.MdiParent = parent; // public Mainparent parent=new Mainparent(); where Mainparent is mdi
homeadmin.Show(); // problem is homeadmin doesnt get opened as child form
count = 1;
}
else if (dr["Rights"].ToString() == "2")
{
uid = dr["Username"].ToString();
name = dr["LoginName"].ToString();
role = "User";
Homestaff homestaff = new Homestaff();
this.Hide();
this.MdiParent=parent;
homestaff.Show();
count = 1;
}
NotifyWindow nw = new NotifyWindow("Employee Management System", "Welcome " + EMP_loginpage.name);
nw.SetDimensions(250, 200);
nw.Notify();
}
else
{
NotifyWindow nw = new NotifyWindow("Employee Management System", "Incorrect Username or password");
nw.SetDimensions(250, 200);
nw.Notify();
MessageBox.Show("User Name Or Password Is Incorrect Please Enter Again", "Wrong Login", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error Message Handled By Try Catch", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
con.Close();
}
}
else
{
MessageBox.Show("Sorry : " + EMP_loginpage.name + " You are already login with the UserID : " + EMP_loginpage.uid + " .So Please first Close Application Or Logout From the Main form", "Stop Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}