Help me to getting value in another form (C#)

I have 2 form: form1; form2
in form 1: textBox1 and Button1

code Button1_Click(){
form2 f = new form2();
f.show()}

help me to get value in textBox1 (form1) at form2

[190 byte] By [ctlatd2] at [2007-12-16]
# 1

I have 2 form: form1; form2
in form 1: textBox1 and Button1

code Button1_Click(){
form2 f = new form2();
f.show()}

help me to get value in textBox1 (form1) at form2

thanks!

ctlatd2 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Hi ctlatd2,

C# is an object oriented language, evry form is a class.

You Need To this :

1. In form2 you need to writh a PUBLIC function that return a value.

2. In the new form "f" you need to call the function like this :

a. ((FrmMain)this.MdiParent).fnGetValue(); // if it's mdi form

b. Form2 f= new Form2() ;
f.fnGetValue() // in your case

Best Regards,
Boaz Shalev.

BoazShalev at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Hi,
You could place a public method in one of your form. And just call it to get the value.
in Form1:

public string getStringValue() {
return myText.Text;
}

in your button click:

Button1_Click(){
form2 f = new form2();
f.Owner = this;
f.show()
}

in your Form2:

Form2() {
InitializeComponents();
Text1.Text = ((Form1) this.Owner).getStringValue();
}

It's only a pseudo code, so I hope that you got what im trying to explain...
cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Either you add a parameter of type TextBox to your Form2's constructor, so when you create an instance of your Form2, you simply calling it as:

private void button1_Click()
{
Form2 f = new Form2(textBox1);
f.Show();
}


Or simply change the modifier of your textBox1 from private (default) to internal; so your Form1's textBox1 will be visible for all the Forms within your project.

Example:

private void button1_Click()
{
Form2 f = new Form2(textBox1);
f.Tag = this;
f.Show();
}

On your Form2, you can simply make a call:

((Form1)Tag).textBox1

To get the TextBox from Form1.
-chris

gwapo at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
thank you alot!
...
I try in form1
public string getStringValue() {
return myText.Text;
}

Button1_Click(){
form2 f = new form2();
f.frm = this;
f.show();
}
in form2:

public Form1 frm;
Form2_Show(){
MessageBox.Show(frm.getStringValue());
}

that ok!

ctlatd2 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...