C# 如何检查是否在另一个表单上选中了复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1410923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:15:27  来源:igfitidea点击:

C# How can I check to see if a checkbox is checked on another form?

c#checkbox

提问by Jamie

I'm using C# and I'd like to check to see if a checkbox on the main form is checked and if so run some code, the problem is I'm in a class file (file with no form, is class file correct?). What is the easiest way to do this?

我正在使用 C#,我想检查主窗体上的复选框是否被选中,如果选中,则运行一些代码,问题是我在一个类文件中(没有表单的文件,类文件是否正确) ?)。什么是最简单的方法来做到这一点?

Thanks Jamie

谢谢杰米

采纳答案by Adam Robinson

The best option is to create a booleanproperty on the Form that exposes the Checkedvalue of the CheckBox.

最好的办法是创建一个boolean是公开的形式对财产Checked的价值CheckBox

public bool OptionSelected
{
    get { return checkBox.Checked; }
    set { checkBox.Checked = value; } // the set is optional
}

回答by Jon Skeet

You need a reference to the form, and the form has to expose the checkbox (or a property which consults the checkbox).

您需要对表单的引用,并且表单必须公开复选框(或查询复选框的属性)。

There's no difference between UI programming and non-UI programming in this respect. How would ask for the Nameproperty of a Personinstance from a different class? You'd get a reference to the instance, and ask for the relevant property.

UI 编程和非 UI 编程在这方面没有区别。如何从不同的类中请求实例的Name属性Person?您将获得对该实例的引用,并请求相关属性。

So you definitely need a reference to the form, and then it's one of:

所以你肯定需要对表单的引用,然后它是以下之一:

bool checked = form.IsAdultCheckbox.Checked;
bool checked = form.IsAdult; 

(Where the IsAdultproperty would return someCheckbox.Checked.)

IsAdult财产将返回的地方someCheckbox.Checked。)

The actual property names may be wrong here (e.g. Checkedmay not return a bool) but I hope you get the idea.

此处的实际属性名称可能是错误的(例如Checked可能不会返回 a bool),但我希望您明白这一点。

回答by Paul Williams

Can you define an interface with a property, have the form implement the interface and return true if the checkbox is checked, and pass an instance of this interface to your class?

您能否定义一个带有属性的接口,让表单实现该接口并在选中复选框时返回 true,并将此接口的实例传递给您的类?

For example:

例如:

interface IMyFormFlag
{
    bool IsChecked { get; }
}

public class MyForm : Form, IMyFormFlag
{
    CheckBox chkMyFlag;

    bool IsChecked { get { return chkMyFlag.Checked; } }
}

public class MyObject
{
    public void DoSomethingImportant(IMyFormFlag formFlag)
    {
        if (formFlag.IsChecked)
        {
            // do something here
        }
    }
}

回答by Magical Mayhem 007

Personally, I don't like using set or get. I did it like this:

就个人而言,我不喜欢使用 set 或 get。我是这样做的:

if (checkBox.IsChecked.Equals(true))
{
//insert code here
}

回答by Amr Angry

you can use this it works fine for me

你可以使用它,它对我来说很好用

    if (Convert.ToBoolean(CheckBox1.IsChecked))
      {
         MessageBox.Show("true");
      }
    else
       {
         MessageBox.Show("false"); 
       }