C # How can I check if a checkbox is checked in a different form?

I use C #, and I would like to check if the checkbox is checked in the main form, and if I run some code this way, the problem is that I am in the class file (file without form, class file correctly?) What is the easiest way to do this?

Thanks Jamie

+3
source share
5 answers

The best option is to create a property booleanin the form that provides the value Checkedfor CheckBox.

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

You need a link to the form, and the form must check the box (or the property that refers to the box).

, UI. Name Person ? .

, , :

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

( IsAdult someCheckbox.Checked.)

(, Checked bool), , .

+1

, true, , ?

:

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
        }
    }
}
+1

set get. :

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

,

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

Source: https://habr.com/ru/post/1531467/


All Articles