Pass Data between two Winforms
December 16, 2011 Leave a comment
- Create a Winform Application
- Create 2 Forms Form1,Form2
- Initialize Form2 in Form1
- Form2 f2 = new Form2();
- f2.ShowDialog();
- Create a Property Name in Form2 Constructor Assign some value
- you can get the property value using f2.Name in Form1//Form2
public partial class Form2 : Form
{
public string Name { get; set; }
public Form2()
{
InitializeComponent();
Name = “DoItDotnet”;
}
}//Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 f2 = new Form2();MessageBox.Show(f2.Name.ToString());
//This will just display the Form Name in MessageBox We need to access the properties inside the Form2 for desired result
MessageBox.Show(f2);
}
}