1

I have a class named "tables", and a winform "Form1", on the form is a button called "table3". The code in the "tables" class runs in a separate thread and needs access to the button on the main form, this is what I have:

Form1.ActiveForm.BeginInvoke(
(Action)(() =>
{
    Form1.ActiveForm.Controls["table3"].Text = "test";
}));

I'm getting a null reference exception.. The name of the button is correct. there's some other problem and I'm not sure what it could be :/

user2864613
  • 169
  • 1
  • 4
  • 10

2 Answers2

0

Try this:

Invoke(new Action<object>((args) =>
{
    Form1.ActiveForm.Controls["table3"].Text = "test";
}));
Jerry
  • 4,258
  • 3
  • 31
  • 58
  • e.Argument - e does not exist in the current context – user2864613 Oct 17 '13 at 11:08
  • OK, I edited the answer. You can also use it in a backgroundworker as in the answer here: http://stackoverflow.com/questions/16373299/issue-with-drag-and-drop – Jerry Oct 17 '13 at 11:09
  • Using a backround worker would require a whole lot of my code to be modified.. hmm – user2864613 Oct 17 '13 at 11:13
  • OK, as in the edit, just remove the second argument. – Jerry Oct 17 '13 at 11:15
  • Still doesn't work, but I'll go with the backround worker, thanks for the advice. :) – user2864613 Oct 17 '13 at 11:18
  • You mean you're getting an error? or doesn' update gui. I guess null refernce would mean that "Form1.ActiveForm.Controls["table3"]" may be null. Try a breakpoint there and use watch or pin to source – Jerry Oct 17 '13 at 11:22
0

If you're getting a NullReferenceException, then a reference is null. So either Form1.ActiveForm == null, or Form1.ActiveForm.Controls == null, or Form1.ActiveForm.Controls["table3"] == null.

I assume Form1 is the name of the class and ActiveForm is a static property.

Henrik
  • 23,186
  • 6
  • 42
  • 92