I am working on c# windows application. I have 2 forms. Form1 has one textbox and one button. If you click on the form1 button it should be displayed form2. And Form 2 also having one button and one text box. In form2 we have to enter some text in the textbox and click the button. The text should be displayed in the form1 textbox. Both forms should be active. We have to refresh form1 textbox. How?
Here I am doing like this: but form1 textbox is not refreshing....
public partial class Form1 : Form { public Form1() { InitializeComponent(); if (PropNearest != string.Empty) { this.textBox1.Text = PropNearest; } } public string PropNearest { get { return this.textBox1.Text; } set { this.textBox1.Text = value; } } private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } } This is form2 code: public partial class Form2 : Form { Form1 f1 = new Form1(); public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { f1.PropNearest = textBox1.Text; f1.Refresh(); } }
theLizardPosted Mar 24, 2010, 4:07 AM
ramesh pPosted Mar 24, 2010, 3:41 AM
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
}
public partial class Form2 : Form
{
Form _frm;
public Form2(Form frm)
{
_frm=frm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objMain = (Form1)_frm ;
objMain.textBox1.Text = "Hello";
}
}
In this technique, i pass the form1 to Form2 in constructor, then create an object of existing Form1 and change there value
ramesh pPosted Mar 24, 2010, 2:46 AM
Please see my requirement:
I have 2 forms form1 and form2. First when you run the application form1 will open. In form1 there is one text box and one button. Do not enter any text in that textbox. Please just click on the button. After clicking the button form2 should be displayed. But, do not close form1. In form2 also there is one text box and one button. Now enter some text in the form2 textbox and click the button. The text should be displayed in the form1 textbox.
BangaruBabu PuretiPosted Mar 24, 2010, 2:31 AM
Step1: Declare Variables in frmChild1
======================================
Public static String fc1var1="February Fever";
Public static String fc1var2="Only at www.hyderabadtechies.info";
step2:Declare Variables in frmChild2
====================================
public static string getval1fromf1="";
public static string getval2from1="";
step3:Declare Variables in frmChild3
====================================
public static string getval1fromf1="";
public static string getval2from1="";
step4:Place two Buttons on frmChild1 --one with id(Go2F1) and two with id(Go2F2)
==========================================
UnderGo2F1_Click
----------------
this.Hide();
//Creating Instance for second child form
frmchild2 f2 = new frmchild2();
//Assigning firstChilvariable values to secondchild form values
f2.getval1fromf1=fc1var1;
f2.getval2fromf1=fc1var2;
f2.Show();
UnderGo2F2_Click
----------------
this.Hide();
//Creating Instance for second child form
frmChild3 f3= new frmChild3();
f3.getval1fromf1=fc1var1;
f3.getval2fromf1=fc1var2;
f3.Show();
step5:Know in frmchild2 place two textboxes(say txtname,txtmicusergroup)
==========================================
Under frmchild2_Load (Same as for frmchild3_Load)
--------------------
textname.text=getval1fromf1.ToString();
txtmicusergroup=getval2fromf1.ToString();
Then frmchild2 textbox controls Displays February Fever/Only at www.hyderabadtechies.info
I hope this will help You
Happy Programming
theLizardPosted Mar 23, 2010, 9:27 PM
{
public TextBox t = null; //declare a surrogate text box, make it form global
public f2()
{
InitializeComponent();
t = te; //this is the text box on the form
}
public partial class Form1 : Form
{
f2 f = new f2();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
f.Show();
}
private void te_KeyUp(object sender, KeyEventArgs e)
{
f.t.Text = te.Text; //te is the text box on form, what ever you enter in this text box will show in the for 2 text box as you enter it,
}
}
}
please don't say it does not work, I know it does...
ramesh pPosted Mar 23, 2010, 4:31 PM
Irshad IbrahimPosted Mar 23, 2010, 10:23 AM
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
the functions Show() and ShowDialog() seems same; but the effect of both are different.