Getting listbox items to display to another form

↘锁芯ラ 提交于 2020-01-06 14:01:56

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/19344550/getting-listbox-items-to-display-to-another-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!