从 Form2 访问 Form1 控件 [英] Access Form1 controls from Form2

查看:68
本文介绍了从 Form2 访问 Form1 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种形式-Form1 &Form2.Form1 有一个按钮(btnNew),它在单击时打开 Form2,并被禁用.我需要再次启用该按钮,只有在 Form2 关闭时.用户还需要同时使用 Form1.此代码不会再次启用该按钮.我在哪里失踪了.

在 Form1 中:

Have two forms-Form1 & Form2.Form1 has a button(btnNew),which opens Form2 on click, and is disabled.I need to enable the button again, only when Form2 is closed.User needs to use Form1 also simultaneously.This code is not enabling the button again. Where am I missing.

In Form1:

private void btnNew_Click_1(object sender, EventArgs e)
  {   
    Form2 f2 = new Form2();
    f2.Show();
    btnNew.Enabled = false;
  }   
public void EnableButton()
 {
    btnNew.Enabled = true;
 }

在 Form2 中:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  {
    Form1 f1 = new Form1();
    f1.EnableButton();
  }

推荐答案

从实例化它的类 (Form1) 中订阅您的 Form2 关闭事件.

Subscribe to your Form2 closing event from within the class that is instantiating it (Form1).

private void btnNew_Click_1(object sender, EventArgs e)
  {   
    Form2 f2 = new Form2();
    f2.Closing += f2_Closing;
    f2.Show();
    btnNew.Enabled = false;

  }   

public void f2_Closing(object sender, FormClosingEventArgs e)
{
   this.EnableButton();
}

public void EnableButton()
 {
    btnNew.Enabled = true;
 }

这篇关于从 Form2 访问 Form1 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆