如何在通过子窗体调用时使用启动窗体中的方法启用其控件?
How to use a method from the Startup form to enable its controls when called through a child form?
我正在尝试在我的启动表单上启用管理员权限。为此,我创建了一个管理工具条登录名,它会启动一个密码表单。但是,我无法通过密码表单更新启动表单。我在这里 [Why the controls not update, when call method from other form 看过一篇类似的文章,但这并没有帮助我解决我的问题。我的代码如下,到目前为止我所取得的成就如下:
// Code for startup form...
public partial class StartupForm : Form {
private void adminToolStripMenuItem_Click(object sender, EventArgs e) {
FrmAdminPassword frmAdminPassword = new FrmAdminPassword();
frmAdminPassword.Show();
//this.Close();
//AdminLogin();
}
public void AdminLogin() {
loginToolStripMenuItem.Enabled = false;
logoutToolStripMenuItem.Enabled = true;
cmb1.Enabled = true;
btn1.Enabled = true;
tab1.Enabled = true;
tab2.Enabled = true;
tabControl1.TabPages.Add(tabAdminTasks);
MessageBox.Show("Admin Logged In");
}
}
// Code for password form
public partial class FrmAdminPassword : Form {
private void btnLoginAdmin_Click(object sender, EventArgs e) {
if (mskAdminPassword.Text == "password") {
StartupForm frm = new StartupForm();
frm.Show();
frm.AdminLogin();
this.Close();
}
else {
MessageBox.Show("Not a valid password");
this.Close();
}
}
}
如果我以这种方式实现它,会发生的情况是启动表单的原始实例仍然作为重复实例存在,所有选项卡和控件都被禁用,并且启动一个新表单并启用所有控件。
其实我想达到的是:
- 单击
adminToolStripMenuItem
。
- 启动
FrmAdminPassword
。
- 输入密码并点击登录
FrmAdminPassword
。
- 点击登录后,关闭
FrmAdminPassword
并启用 StartupForm
上的控件。
有人可以帮忙吗?谢谢。
发生的事情是您实例化了两个单独的表单副本。在启动时,启动表单被实例化。在登录期间,您正在实例化一个全新的副本。您的登录需要参考现有的启动表单。
使用ShowDialog()显示您的登录表单。这将停止启动表单中代码的执行,直到登录表单关闭
private void adminToolStripMenuItem_Click(object sender, EventArgs e)
{
// Putting the creation of the form inside a using block allows
// the automatic closing and disposing of the form when the code
// reaches the closing brace of the using block.
using(FrmAdminPassword frmAdminPassword = new FrmAdminPassword())
{
// This opens the frmAdminPassword form in modal mode, no
// code is executed after the call until you close the
// form with DialogResult.OK, DialogResult.Cancel or whatever
if(DialogResult.OK == frmAdminPassword.ShowDialog())
{
MessageBox.Show("Login executed with success!");
}
else
{
// Mo password given or form cancelled
// put here the logic to exit or disable things
}
}
}
现在在登录表单中单击“确定”按钮,您可以执行您的逻辑来验证密码并在密码匹配时允许关闭表单
public partial class FrmAdminPassword : Form
{
private void btnLoginAdmin_Click(object sender, EventArgs e)
{
if (mskAdminPassword.Text == "password")
this.DialogResult = DialogResult.OK;
else
{
MessageBox.Show("Not a valid password");
this.DialogResult = DialogResult.None;
}
}
}
要完成这项工作,您需要设置 DialogResult property of the Login Form to something different from DialogResult.None。这将强制登录表单自动隐藏(而不是关闭),因此您可以阅读启动表单初始实例上的 DialogResult
属性 并决定下一步做什么。您还应该提供一些方法来使用 DialogResult.Cancel
退出登录表单并停止进一步处理,因为用户决定不提供密码。
试试这个...
FrmAdmin 密码
private void btnLoginAdmin_Click(object sender, EventArgs e)
{
if (mskAdminPassword.Text == "password")
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
else
{
MessageBox.Show("Not a valid password");
this.Close();
}
}
启动表单
private void adminToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmAdminPassword frmAdminPassword = new FrmAdminPassword();
using(frmAdminPassword)
{
if(frmAdminPassword.Show() == System.Windows.Forms.DialogResult.OK)
{
AdminLogin();
}
}
}
我正在尝试在我的启动表单上启用管理员权限。为此,我创建了一个管理工具条登录名,它会启动一个密码表单。但是,我无法通过密码表单更新启动表单。我在这里 [Why the controls not update, when call method from other form 看过一篇类似的文章,但这并没有帮助我解决我的问题。我的代码如下,到目前为止我所取得的成就如下:
// Code for startup form...
public partial class StartupForm : Form {
private void adminToolStripMenuItem_Click(object sender, EventArgs e) {
FrmAdminPassword frmAdminPassword = new FrmAdminPassword();
frmAdminPassword.Show();
//this.Close();
//AdminLogin();
}
public void AdminLogin() {
loginToolStripMenuItem.Enabled = false;
logoutToolStripMenuItem.Enabled = true;
cmb1.Enabled = true;
btn1.Enabled = true;
tab1.Enabled = true;
tab2.Enabled = true;
tabControl1.TabPages.Add(tabAdminTasks);
MessageBox.Show("Admin Logged In");
}
}
// Code for password form
public partial class FrmAdminPassword : Form {
private void btnLoginAdmin_Click(object sender, EventArgs e) {
if (mskAdminPassword.Text == "password") {
StartupForm frm = new StartupForm();
frm.Show();
frm.AdminLogin();
this.Close();
}
else {
MessageBox.Show("Not a valid password");
this.Close();
}
}
}
如果我以这种方式实现它,会发生的情况是启动表单的原始实例仍然作为重复实例存在,所有选项卡和控件都被禁用,并且启动一个新表单并启用所有控件。
其实我想达到的是:
- 单击
adminToolStripMenuItem
。 - 启动
FrmAdminPassword
。 - 输入密码并点击登录
FrmAdminPassword
。 - 点击登录后,关闭
FrmAdminPassword
并启用StartupForm
上的控件。
有人可以帮忙吗?谢谢。
发生的事情是您实例化了两个单独的表单副本。在启动时,启动表单被实例化。在登录期间,您正在实例化一个全新的副本。您的登录需要参考现有的启动表单。
使用ShowDialog()显示您的登录表单。这将停止启动表单中代码的执行,直到登录表单关闭
private void adminToolStripMenuItem_Click(object sender, EventArgs e)
{
// Putting the creation of the form inside a using block allows
// the automatic closing and disposing of the form when the code
// reaches the closing brace of the using block.
using(FrmAdminPassword frmAdminPassword = new FrmAdminPassword())
{
// This opens the frmAdminPassword form in modal mode, no
// code is executed after the call until you close the
// form with DialogResult.OK, DialogResult.Cancel or whatever
if(DialogResult.OK == frmAdminPassword.ShowDialog())
{
MessageBox.Show("Login executed with success!");
}
else
{
// Mo password given or form cancelled
// put here the logic to exit or disable things
}
}
}
现在在登录表单中单击“确定”按钮,您可以执行您的逻辑来验证密码并在密码匹配时允许关闭表单
public partial class FrmAdminPassword : Form
{
private void btnLoginAdmin_Click(object sender, EventArgs e)
{
if (mskAdminPassword.Text == "password")
this.DialogResult = DialogResult.OK;
else
{
MessageBox.Show("Not a valid password");
this.DialogResult = DialogResult.None;
}
}
}
要完成这项工作,您需要设置 DialogResult property of the Login Form to something different from DialogResult.None。这将强制登录表单自动隐藏(而不是关闭),因此您可以阅读启动表单初始实例上的 DialogResult
属性 并决定下一步做什么。您还应该提供一些方法来使用 DialogResult.Cancel
退出登录表单并停止进一步处理,因为用户决定不提供密码。
试试这个...
FrmAdmin 密码
private void btnLoginAdmin_Click(object sender, EventArgs e)
{
if (mskAdminPassword.Text == "password")
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
else
{
MessageBox.Show("Not a valid password");
this.Close();
}
}
启动表单
private void adminToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmAdminPassword frmAdminPassword = new FrmAdminPassword();
using(frmAdminPassword)
{
if(frmAdminPassword.Show() == System.Windows.Forms.DialogResult.OK)
{
AdminLogin();
}
}
}