c# - Can't edit Form1 textbox from Form2 -


this question has answer here:

in form1 have textbox filled char array values (*). in form2 change 1 of array values (change * letter) , have update form1 textbox. when press button - nothing happens. form2 code snippet:

 private void button2_click(object sender, eventargs e)         {             ....              ....              letter = (char)form1.number;               textbox1.text = letter.tostring(); //shows me letter i'm replacing '*'         }   private void button3_click(object sender, eventargs e)         {             frm1 = new form1();              form1.chararray[convert.toint32(frm1.numericupdown2.value)] = letter; //which array value i'm changing              frm1.textbox2.text = string.empty;              (int = 0; < frm1.numericupdown1.value; i++)             {                 frm1.textbox2.text += form1.chararray[i];             }         } 

instead of editing form1-window talking about, creating new (yet invisible) 1 in event-handler , perform operations on that.

solution:

as doing fields (chararray, number), have give method "access" instance of form1 exists!

option 1: have done chararray , number, add static field form1 (something public static form1 instance;), , assign correct instance. example, put form1.instance = this; inside of form1's constructor.

option 2: wherever creating , showing form2-window (i guess there new form2().show() in code , bet has access instance of form1 - maybe within form1), pass right instance of form1 constructor argument, i.e. change form2's constructor accepts parameter of type form1 , call new form2(therightinstanceofform1).

summary: although "option 2" cleaner one, guess give more trouble "option 1": not did define several static fields (so not make difference add one), if using visual studio designer design form2, break change constructor...


Comments

Popular posts from this blog

renaming files in a directory using python or R -

c# - ajax - How to receive data both html and json from server? -