【发布时间】:2017-12-05 15:15:04
【问题描述】:
我的程序中有几个类,我正在使用 Windows 窗体从不同的类创建对象并将它们列在不同的列表框中。
现在我有一个包含所有列表框的表单(表单 1)和另一个表单(CreerVoiture),我将所有信息放入其中以创建我选择的对象。
一个例子是当我点击按钮“Voiture!”我的第二个表单打开,当我添加所有信息并按下“Ajouter”时,它会将对象添加到选定的列表框中。
Form1 中的代码:
private void button1_Click(object sender, EventArgs e)
{
CreerVoiture creervoiture = new CreerVoiture();
if (creervoiture.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of form2's TextBox.
Voiture voiture = new Voiture(creervoiture.GetMarque(), creervoiture.GetPrix(), creervoiture.GetConsommation(), creervoiture.GetReservoir());
this.list_voiture.Items.Add(voiture);
this.list_voiture.DataSource = null;
}
creervoiture.Dispose();
}
我想知道的是,在将第二个表单中添加的参数添加到列表框后,如何访问它。
我正在考虑使用类似的东西:
list_voiture.SelectedItem.Prix
Prix 是我班 Voiture 的吸气剂
public double Prix
{
get { return this.prix; }
set { this.prix = value; }
}
但这似乎是不可能的。这可能吗?如果可以,怎么做?
提前致谢,
杰里米
【问题讨论】:
-
使用
ListBox.Items遍历ListBox 中的所有项目。如果您只需要 SelectedItem,则使用ListBox.SelectedItem属性。此外,您需要将属性转换为Voiture,然后访问属性。 -
您好,谢谢您的回答。您能否详细说明您的答案,我不知道您将属性转换为 Voiture 是什么意思。谢谢
-
var selectedItem = list_voiture.SelectedItem as Voiture
标签: c# winforms class listbox listboxitem