【问题标题】:Enabling button of one form another forms button click启用一种形式的按钮另一种形式的按钮单击
【发布时间】:2011-10-01 05:10:01
【问题描述】:

我同时运行 form1 和 form2。

流程如下

1) Click form1 button
2) disable form1 button
3) show form2 ( form 1 is not closed)
4) click form 2 button 
5) close form 2
6) enable form1 button 

我已经完成了第 5 步。 6th 做不到。有人可以帮忙吗?

【问题讨论】:

  • 您遇到了什么问题导致您无法执行第 6 步?
  • 我实例化了新的 form1 。我认为当表格 1 已经运行时,这不是遵循的方式。还需要做什么?
  • 您真的需要这些表单“同时”运行,还是 form2 是 form1 的子表单?

标签: c# .net winforms button


【解决方案1】:

你是对的 - 创建另一个 Form1 副本不是正确的方法。

您的问题不是很清楚,但听起来您想重新启用在打开 Form2 之前禁用的同一个按钮。在这种情况下,您可以监听Form2FormClosed 事件并在Form1 中处理它:

public class Form1 : Form
{
    public void ShowForm2()
    {
        myButton.Enabled = false;
        var f2 = new Form2();
        f2.FormClosed += HandleForm2Closed;
        f2.Show();
    }

    private void HandleForm2Closed(Object sender, FormClosedEventArgs e)
    {
        myButton.Enabled = true;
    }
}

【讨论】:

  • 尽管@Anthony Pegram 的解决方案解决了我的问题。我觉得这是最好的关注
  • 我同意,我缺乏 winform 开发可能会发光。呃,事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 2016-01-30
  • 2011-09-05
  • 2019-05-07
  • 1970-01-01
  • 2015-04-17
相关资源
最近更新 更多