0

I tried (To edit the text of a TextBox)

public static void test(String s)
{
    mainName.Text = "";
}

But it gives me an error. How can I fix it ? Note: This code is in the form2.cs

Hx0
  • 309
  • 3
  • 9

1 Answers1

0

Try the following.

public static void test(String s)
{
    form2 frm = new form2();
    frm.mainName.Text = "";
}
ax752
  • 142
  • 2
  • 12
  • Sure...that'll compile...but it doesn't do anything useful. Did the text box get updated? I seriously doubt it. – Idle_Mind Dec 21 '14 at 07:07
  • Well if it dosen't work, this should do the trick [Writing to a TextBox from another thread?](http://stackoverflow.com/questions/519233/writing-to-a-textbox-from-another-thread) – ax752 Dec 21 '14 at 15:51
  • Not really a matter of threading, but of references. You need a reference to the Form that is actually on the screen being displayed (the author used the phrase "current form" in the title). When you create a new instance of the Form, it's most certainly not the one that already exists. Long story short, the method shouldn't be static, and the reference to Form2 that is displayed should be passed to where ever the form is being updated from. – Idle_Mind Dec 21 '14 at 19:50