![]() |
|||
.NET Winforms
By: rekha singh | 28 Mar 2010 5:05 pm
Hi All, I was asked in one of the interview on .NET Winforms. Can anybody please help on this?
The question is:
"I have 2 forms, Form1 and Form2. In form1 I have one button. On click of this button form2 should pop up. I'll enter some data in form2.
By closing form2, I should return to form1 and I should get the entered data (in form2) in form1. How I can achieve this? "
Kindly waiting for the response.
Thanks, CommentsHello,
This is pretty simple...
Assume that the first form has a button and a textfield where the data should be displayed, while the second form should have also a button and a textfield where the data should be entered.
In this case the first form should have a public property that sets the value of the textBox. The second form should have the public variable of the Form1 type.
You start the form2 by clicking the button and pass the reference to form1. Now form2 keeps the reference to form1. Then you enter the data in the Form2 text field and click the button. The button first sets the Form1 public property with the data from Fotm2.Text1 and then closes the form. Please see below...
namespace WindowsFormsApplica tion1
{
public partial class Form1 : Form
{
public string showData
{
set { textBox1.Text = value; }
}
public Form1()
{
InitializeComponent ();
}
private void button1_Click( object sender, EventArgs e)
{
Form2 f = new Form2();
f.fTarget = this;
f.ShowDialog( );
f.Dispose();
}
}
}
namespace WindowsFormsApplica tion1
{
public partial class Form2 : Form
{
public Form1 fTarget;
public Form2()
{
InitializeComponent ();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click( object sender, EventArgs e)
{
if (textBox1.Text == "") return;
fTarget.showData = textBox1.Text;
Close();
}
}
}
By: rekha singh | 28 Mar 2010
Here are the steps that you should do to accomplish that Create a new form called Form1 Create a new form called Form 2 >>> from "project" menu click "add windows form" In Form2 drag a text box , it's the text where you will write your text and by default its name will be textBox1 Now open the Program class >> (the class that has the Main method and declare a new instance of Form2 and an instance of Form1 outside the Main and make them both static … your code should look like this: static class Program { public static Form1 frm1; public static Form2 frm2;
[STAThread] static void Main() { Application.EnableVisualStyles (); Application.SetCompatibleTextR enderingDefault(false); Application.Run(new Form1()); } } drag a button to the first form (Form1) and then double click it and write the following code in its event handler Program.frm2 = new Form2(); Program.frm2.Show() ; Into the Form1 class declare a new variable of type string called temp>>>it will be used to store the text returned by Form2 Now this instance of Form2 is global and can be seen anywhere throughout the entire application
You should change the code written into the Main method to be like this static void Main() { Application.EnableVisualStyles (); Application.SetCompatibleTextR enderingDefault(false); frm1 = new Form1(); Application.Run(frm1); } Now open the event handler of the FormClosing event of Form2 and write the following code Program.frm1.temp = this.textBox1.Text; Now when you close the Form2 everything written into the textbox1 will be copied to the temp variable in Form1 >>>> and then you can do whatever you want with that text
I have included the solution in a zip file in the following link
http://www.mediafir e.com/file/ 2udwmiymwcn/ WindowsFormsAppl ication1. zip
By: rekha singh | 28 Mar 2010
|
