c# - How to share data between two Windows Form in Visual Studio 2008 -


m beginner n want help

i have 2 forms

  1. form1
  2. form2

form1 contains label, textbox, button.

form2 contains textbox only.

i want when click on button in form1, text in textbox (form1 textbox) should pass form2 , show in textbox of form2.

code form1.cs

public partial class form1 : form {     public string str;       public form1()     {       initializecomponent();     }     public class global     {         public static string str;      }     private void form1_load(object sender, eventargs e)     {      }      private void button1_click(object sender, eventargs e)     {         str = textbox1.text;      }      private void button2_click(object sender, eventargs e)     {         form2 f2 = new form2();         f2.show();     } } 

code form2.cs

public partial class form2 : form {      public form2()     {         initializecomponent();     }      private void form2_load(object sender, eventargs e)     {         string str1 = form1.global.str;         textbox1.text = str1;      } } 

add class in project. make members of class static (so can called class name).

public class global { public static string strvalue = ""; }

now on button click event assign textbox1.text value string variable in global class.

strvalue = this.textbox1.text; 

now can access textbox1.text value globally using static variable. in form 2 can assign value other text box

this.textbox2.text = global.strvalue; 

note: here in example, assumed textbox1 on form1 ans textbox2 on form2.

edit: have made changes in code. code form1.cs

public partial class form1 : form {            public form1()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {      }      private void button1_click(object sender, eventargs e)     {         global.str = textbox1.text;      }      private void button2_click(object sender, eventargs e)     {         form2 f2 = new form2();         f2.show();     } } public class global {     public static string str;  } 

code form2.cs

public partial class form2 : form {      public form2()     {         initializecomponent();     }      private void form2_load(object sender, eventargs e)     {                     textbox1.text = global.str;         } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -