使用复选框从表单 1 禁用表单 2 上的按钮 [英] disabling a button on form 2 from form 1 with a checkbox

查看:21
本文介绍了使用复选框从表单 1 禁用表单 2 上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表单对象:form1form2.

I have two form objects: form1 and form2.

我在 form2 上有 1 个按钮,在 form1 上有一个复选框.当复选框被选中时,我想显示按钮,当它被取消选中时,我想禁用该按钮.我知道在 Visual Basic 中我做了这样的事情:

I have 1 button on form2 and a check box on form1. When the check box is checked, I want to show the button and when it is unchecked I want the button to be disabled. I know that in visual basic I did such a thing like this:

form2.button.visible = false

我将如何在 c# 中做这样的事情?

How would I do something like this in c# ?

推荐答案

In 一般情况(当 Form1From2 实例是独立的)你可以做类似的事情.在Form2 中实现一个公共属性:

In general case (when Form1 and From2 instances are independent) you can do something like that. In Form2 implement a public property:

  public partial class Form2 {
    ...
    public Boolean IsMyButtonVisible {
      get {
        return myButton.Visible;
      } 
      set {
         myButton.Visible = value; 
      } 
    } 
    ...
  }

myCheckBox 上的 Form1 CheckedChanged 中找出 Form2 实例并分配属性:

In Form1 on myCheckBox CheckedChanged find out Form2 instances and assign the property:

  public partial class Form1 {
    ...
    private void myCheckBox_CheckedChanged(object sender, EventArgs e) {
      foreach(Form f in Application.OpenForms) {
        Form2 form2 = f as Form2; 

        if (form2 != null)
          form2.IsMyButtonVisible = myCheckBox.Checked;
      }
    }
    ...
  } 

这篇关于使用复选框从表单 1 禁用表单 2 上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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