0

I have external class for do some work with my form. I have some error end can't handle with it.

My first variant

mainForm.CheckBox1.Checked = true;

it doesn't worked with an error
Cross-thread operation not valid: Control 'CheckBox1' accessed from a thread other than the thread it was created on

So I tried like in folow post stackoverflow question 1 but when I wrote

mainForm.CheckBox1.IsCheked = true

Compiler gives an error that
The error is 'System.Windows.Forms.Control' does not contain a definition for 'IsChecked' and no extension method 'IsChecked' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?)

I saw and found answer in srackoverflow question 2

But i can't casting my object because when I write

(CheckBox)mainForm.CheckBox1.IsCheked = true

it gives an error Can't find name of or namespace (are you missing a using directive or an assembly reference?) for CheckBox. I have using System.Windows.Forms; in the beginning of class.

I'm a beginner in C# so may your give me some suggestions what I do wrong?

Community
  • 1
  • 1
user2105282
  • 724
  • 1
  • 12
  • 26
  • is it necessary to set the checked state during the thread function? (or can it be done after completion) – Sayse Apr 21 '13 at 10:16
  • I have some static function, which is called from MainForm. Something like `Dialogs.setCheckBox();` where Dialog is external class with functions. Is it necessary to create new thread for setting the CheckBox? – user2105282 Apr 21 '13 at 10:23
  • It sounds like your already on a separate thread, "Cross-thread operation" if your trying to set the checkbox on a static function then you must pass the checkbox you want to set into the SetCheckBox parameters – Sayse Apr 21 '13 at 10:25

2 Answers2

1

If I'm understanding correctly you have a couple of choices...Static functions do not have an instance associated to them so they will have no understanding of what your checkbox is, If you wish to keep your static function then you need to pass in the checkbox to your static function although this is not very practical..

public static void SetCheck(Checkbox c, bool value)
{
    c.Checked = value;
}

The reason this is not practical is because if you know the checkbox then you can probably just set the checkbox anyway, you could provide a Checked property on your main form to only allow the checked value of your checkbox to be changed.

public bool IsMyCheckboxChecked
{
    get{ return checkBox1.Checked;}
    set{ checkBox1.Checked = value;}
}

If this is your parent form you are trying to set, you could then always access this via

((MainForm)this.ParentForm).IsMyCheckboxChecked = true

Edit As method

public void SetCheckbox(int num, bool state)
{
    checkboxList[num].Checked = state;
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Thanks for answer, I'll try. But can I use IsMyCheckboxChecked with couple of chekboxes, not just with one? Something like `public bool IsMyCheckboxChecked(CheckBox, cbNumber){ ... };` – user2105282 Apr 22 '13 at 05:51
  • To do that you would need your checkboxes stored in a list or array and then use a method such as my edit, otherwise you could use reflection but that would be overkill – Sayse Apr 22 '13 at 06:38
  • Thanks for your help. After I did what you said, I had an error about threads. So i use delegate to access to my checkBoxes. Some basic example of delegate can be found here http://msdn.microsoft.com/ru-ru/library/system.windows.forms.methodinvoker.aspx – user2105282 Apr 26 '13 at 13:17
0

create a constructor for your second class with a CheckBox parameter.

    private readonly CheckBox _externalCheckBox = null;
    public SecForm(CheckBox externalCheckBox)
    {
        _externalCheckBox = externalCheckBox;
        InitializeComponent();
    }

so in mainForm create an instance of SecForm and pass the checkBox for it and chenge Checked Property in secForm.

    private void ChangeCheck(bool value)
    {
        _externalCheckBox.Checked = value;
    }
Ali Jalali
  • 145
  • 1
  • 3
  • 10