Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

Passing values between Winforms – C# (Tried all ways..!!)

Hi all,
 Back 2 basic …yes.but interesting…I had a doubt in my mind for very long time about sharing a values between 2 winforms…Most of know in webforms…but this is something diff…I had the same kind of requirement in Dicom viewer project but I simply solved it for time being without clearing myself..but here I’ve cleared and tried all possible ways. pls free to comment here if I’m wrong or anyother ways of doing the same.
 
(1) Set the access modifier to Public for that control to access in Form1
In form1
Form2 f=new Form2();
f.textBox1.Text =textBox1.Text;
f.Show();
In form2
 public System.Windows.Forms.TextBox textBox1;
 
 (2) By using a Public property in Form2
 In form1
 Form2 f=new Form2();
f.textBox1.Text =textBox1.Text;
 f.Show();
In form2
private string m_TempText = "";
public string TempText
{
get { return m_TempText; }
 set { m_TempText = value; textBox1.Text = m_TempText; }
 }
 
 (3) Passing thru Form2 contructor
In form1
Form2 fg=new Form2(textBox1.Text);
fg.Show();
 In form2
public Form2(string s)
{
InitializeComponent();
textBox1.Text =s;
 }
 
(4) Assiging the Form2 object with Form1 Object
 In form1
public System.Windows.Forms.TextBox textBox1;
 Form2 f=new Form2();
 f.frm1inForm2=this;
 f.ShowDialog();
 In form2
  public class Form2 { public Form1 frm1inForm2; }
  private void Form2_Load(object sender, System.EventArgs e) { this.textBox1.Text = ((Form1)    this.frm1inForm2).textBox1.Text; }
 
 If you guys knows anyother method for doing the same thing…pls share it here..
 
Mahesh
 

Advertisements

May 3, 2006 - Posted by | .NET General

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: