简体   繁体   中英

Getting listbox items to display to another form

I'm still new here so please consider. My question is that I have two forms, (form1 and form2). In form1 there's a listbox with customers names on it. Now what i want is when the user clicks a name on the listbox, a new form (form2) pops up and displays the rest of the information of the customer (age,address,phone number) on the textboxes. When I click a name on the listbox it displays the rest of the info but only on that form, I can't get it to display on another form. I'm coding it using Visual studio, C#. Any help would be appreciated. Thanks!

You have to pass information from form1 to form2 in one way or another. If the data for customers is collected within a class, you can simply pass the object that corresponds to the selected index in the listbox.

For instance if your customer data in form1 is set up like this:

List<CustomerData> Customers { get; set; }

And each object in that list corresponds to its respective index in the listbox, then you would need a form2 constructor similar to this:

public form2(CustomerData customer){
  // set all form data here based on the customer
}

Potentially, if you asign the passed object in form2, you can manipulate the object within form2 and it will automatically update in form1 as well (assuming it is a class).

Then create your listbox click event method and open the form, passing the selected customer:

if(listbox.SelectedIndex < 0) return;
form2 f = new form2(Customers[listbox.SelectedIndex]);
f.Show();

I hope this is what you were looking for. A bit difficult to understand from your original question.

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