1

Possible Duplicate:
sharing data between forms

I have:

public partial class LoginForm : Form
{
    private string somedata = "somedata";

    public LoginForm()
    {
        InitializeComponent();
    }    
}

I want LoginForm to open another form and send somedata to it. How can I do this?

Community
  • 1
  • 1
Ichibann
  • 4,371
  • 9
  • 32
  • 39

3 Answers3

4

You could do something like the following. It assumes that you've created a form called SomeForm and have added a constructor that accepts a string to it.

    public partial class LoginForm : Form
    {
        private string somedata = "somedata";

        public LoginForm()
        {
            InitializeComponent();

            OpenForm(somedata);
        }    
    }

    private void OpenForm(string Data)
    {
        SomeForm sf = new SomeForm(Data);
        sf.Show();
    }
Michael Todd
  • 16,679
  • 4
  • 49
  • 69
4

First create a public string on your second form you want to pass the data from login:

   public partial class Form2 : Form       
 {  

 public Form2()              
{  
InitializeComponent(); 
}  

    public string messagefromLogin;
    MessageBox.Show(messagefromLogin);

    }

then on your login:

 public partial class LoginForm : Form       
 {  

 public LoginForm()              
{  
InitializeComponent(); 
}   
private string somedata = "somedata";

//Show Form2 and pass the string "somedata"
 private void btnShowForm2_Click(object sender, EventArgs e)
        {
            var frm2 = new Form2{messagefromLogin=somedata}
frm2.Show();
        }

}

Regards

BizApps
  • 6,048
  • 9
  • 40
  • 62
2

there are many ways,

Pass a reference of your parent form to your child or just pass the data to the child form in the constructor or the set a property.

protected void viewHelp(){
  var loginHelp = new LoginHelpForm();
  loginHelp.ParentForm = this;
  loginHelp.Show(); 
  this.Hide();
}
Nickz
  • 1,880
  • 17
  • 35