Question

I've just about got this but I'm not doing something right. I'm trying to pass a value from form1 to form2. On form2 I've got a property set up allowing access to one of it's text boxes. On form1 I've got it set to open an instance of form2 and pass a value from an object in a listbox to form2's text box. It seems like I've got things set up almost right because I tested it by posting the object value in a messagebox.show and it displayed the different object values just how I planned. For some reason though when I actually run it form2 will open but it will not set the value I passed to the textbox in the form, it's just a blank form. I've got no errors but I'm thinking it has something to do with the data not being passed directly to my new instance of form2. I hope I explained it well enough. Any help is appreciated.

form 1

private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmProperties editProperties = new frmProperties();
    editProperties.ShowDialog();

    Employee person = (Employee)lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
    editProperties.TextFirstName = person.EmployeeFirstName;
}

form 2

public string TextFirstName
{
    get { return txtFirstName.Text; }
    set { txtFirstName.Text = value; }
}
Was it helpful?

Solution

You have to set the textbox before you show the dialog.

private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmProperties editProperties = new frmProperties();
    Employee person =   (Employee)lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
    editProperties.TextFirstName = person.EmployeeFirstName;
    editProperties.ShowDialog();    
}

OTHER TIPS

private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmProperties editProperties = new frmProperties();
    editProperties.ShowDialog();

    Employee person = new   Employee ();
person.EmployeeFirstName = lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
    editProperties.TextFirstName = person.EmployeeFirstName;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top