Mình đang làm 1 bài tập về User Control trên Window Form.
Form 1 có nút Login mình click vào nút đó thì gọi Form 2 lên là form login. Ở form 2 chỉ có 1 user control mình thiết kế trước . Ở đây mình không hiểu lắm về nguyên lý hoạt động của Form cha.
Ví dụ Control của mình là login, khi mình hoàn thành gõ vào username, password làm sao mình lấy Form cha mà cái user control nó đang nằm trên để close form ở đây là form 2 .
Đây là code của mình
User Control Login
public partial class LoginControl : UserControl
{
public delegate void Login(string username, string password);
public event Login LoginEvent;
public LoginControl()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUsername.Text.Trim();
string password = txtPassword.Text.Trim();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
MessageBox.Show("Wrong username or password", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Hand);
return;
}
if (LoginEvent != null)
{
LoginEvent(username, password);
// Mình muốn lấy form cha để nhấn nút cancel thì close form ( Form 2 )
}
else
{
// Mình muốn lấy form cha để nhấn nút cancel thì close form ( Form 2 )
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
// Mình muốn lấy form cha để nhấn nút cancel thì close form ( Form 2 )
}
Đây là hình form đã gắn user control và form 1