tags:

views:

775

answers:

4

Hi all,

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?

Thanks Jamie

A: 

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 Name property of a Person instance from a different class? You'd get a reference to the instance, and ask for the relevant property.

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 IsAdult property would return someCheckbox.Checked.)

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

Jon Skeet
@Jamie - Be careful of threads here! If the class is operating in a background thread, don't forget to Invoke back to the main thread.
Paul Williams
+4  A: 

The best option is to create a boolean property on the Form that exposes the Checked value of the CheckBox.

public bool OptionSelected
{
    get { return checkBox.Checked; }
    set { checkBox.Checked = value; } // the set is optional
}
Adam Robinson
Hi Adam, thanks for that, would I place this code on the event when the check box is checked? Thanks
Jamie
Sorry about this but how do I use the code from my class?Thanks
Jamie
You would place this code in the form code. In order to use it from another instance, you would have to have a reference to the form that exposes this property.
Adam Robinson
Thanks Adam, sorry to push for clarity but I've not had to do this before so how would I create a reference to the form? Many thanks, I am trying to read around the subject at the moment but any help would really be appreciated. Thanks
Jamie
Every instance of a class has a reference. The reference is stored in the variable that you use to access that instance. For example, if I had a class called MyClass, doing MyClass x = new MyClass() creates a new instance and places a reference to it in the variable called 'x'. Inside of an instance, like in your form code (Forms are classes, by the way), the 'this' keyword means a reference to the current instance. I can't get any more specific other than to say that you would pass around the reference, usually by supplying 'this' as an argument somewhere, in order to refer to the form.
Adam Robinson
Many thanks, I've managed to do it. Have a great weekend.
Jamie
+1  A: 

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?

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
        }
    }
}
Paul Williams
A: 

Well I also have checkbox on another form and I want to get that value to my main form, but the problem is that no matter whenever its checked or not, it returns me false.

CODE:
// on Form2.cs, which is named Configuration

            public bool _continues;
            public bool continues
            {
                get { return _continues; }
                set { _continues = cBox_interval.Checked; }
            }
// on Form1

 private void button1_Click(object sender, EventArgs e)
        {
            Configuration _conf = new Configuration(); // calling second Form
            bool _continues = _conf.continues; 
            MessageBox.Show(_continues.ToString());
        }

and by default I even setted *cBox_interval.Checked = true* and messagebox still returns false

p2rn4