问题描述
我还是新来的,所以请考虑.我的问题是我有两种形式(form1和form2).在form1中,有一个带有客户名称的列表框.现在我想要的是,当用户单击列表框上的名称时,会弹出一个新表单(form2),并在文本框上显示客户的其余信息(年龄,地址,电话号码).当我单击列表框上的名称时,它会显示其余信息,但仅显示在该表单上,而无法在其他表单上显示.我正在使用Visual Studio C#对其进行编码.任何帮助,将不胜感激.谢谢!
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!
推荐答案
您必须以一种或另一种方式将信息从form1传递到form2.如果客户数据是在一个类中收集的,则只需在列表框中传递与所选索引相对应的对象即可.
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.
例如,如果您的客户数据在form1中是这样设置的:
For instance if your customer data in form1 is set up like this:
List<CustomerData> Customers { get; set; }
该列表中的每个对象都对应于其在列表框中的索引,那么您将需要一个类似于以下内容的form2构造函数:
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
}
可能的是,如果在form2中分配了传递的对象,则可以在form2中操作该对象,并且该对象也会在form1中自动更新(假设它是一个类).
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).
然后创建您的列表框click事件方法并打开表单,传递所选客户:
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.
这篇关于获取列表框项目以显示到另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!