我有兩種形式。一個是MainForm,它是一個MDI父級,並且具有在MainForm加載時設置爲enabled = false
的ToolStripFile,另一個形式是form2,它是一個MDI子級並充當我的登錄表單。如果登錄成功,ToolStripFile應該是enabled = true
。我有這個代碼,但它不起作用:從另一種形式禁用控件
private void btnLogin_Click(object sender, EventArgs e)
{
MainForm mf = new MainForm();
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT * FROM Employees WHERE Username = @Username AND Passcode = @Passcode";
command.Parameters.AddWithValue("@Username", txtUsername.Text);
command.Parameters.AddWithValue("@Passcode", txtPassword.Text);
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
Employees emp = new Employees();
//emp.MdiParent = this.MdiParent;
//emp.Show();
mf.ToolStripFile.enabled = true;
this.Dispose();
}
if (count > 1)
{
MessageBox.Show("There is a duplicate in username and password.");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex, "LOGIN");
}
finally
{
connection.Close();
}
}
什麼是你從這個代碼的執行,恰好?什麼不能工作?甚至顯示'mf'? (我沒有看到'mf.Show()') –
您禁用了一個新創建的MainForm,它無法工作。相反,您應該將對初始MainForm的引用傳遞給「form2」。 – Robert