help needed for passing data between forms ?
-
hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.
-
hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.
-
hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.
Mehmet Fatih Akbulut wrote:
what if i want to use that 'text' in another function or class ?
Then you see to it that the value gets there. There is nothing magical going on, the values are where you put them.
Mehmet Fatih Akbulut wrote:
and what if i have to pass not just one value but many values between forms ???
Then you use more than one parameter or put the values together in a class or struct and pass that along.
Mehmet Fatih Akbulut wrote:
secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ???
You are creating a completely new instance of Form1. That instance will not have the data that you put in the instance of Form1 that already exists. To access the data in the existing instance, you need a reference to it. --- b { font-weight: normal; }
-
hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.
Have a look at this[^] article.
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.
Mehmet Fatih Akbulut wrote:
> To access the data in the existing instance, you need a reference to it. how can i do this ? would you please explain a bit more ? or give a simple example ¿
You can send the reference along when you create the form:
Form2 mehmet = new Form2(this);
And recieve it in the constructor:public Form2(Form1 form1)
--- b { font-weight: normal; } -
hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.
You can use your own custom event and Delegates to achieve the same