简体   繁体   中英

Passing a class value from listbox in form1 to textbox in form2

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; }
}
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;

}

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();    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM