【发布时间】:2014-01-11 12:11:17
【问题描述】:
一直在寻找一个明确的例子。 我创建了一个新对象,包括设置几个属性,将整个对象添加到 listBox,然后写了一个字符串来描述它们。现在,我想要选定索引处的 lsitBox 对象中的一项。有许多语法似乎具有相似但不同的用法,这使搜索变得复杂......
Pseudocode:
SpecialClass object = new SpecialClass;
object.propertyA;
Object.PropertyB;
listBox.Items.Add(object);
//listBox.SelectedItem[get propertyA]? What would retrieve propertyA or propertyB from the //list after putting the object in the list?
....我尝试使用这个变量设置,类似这样...
MRecipeForm parent = new MRecipeForm();
ListViewItem item = new ListViewItem();
item.Tag = parent.recipeListB.Items;
var myObject = (double)parent.recipeListB.SelectedItems[0].Tag;
// here you can access your properties myObject.propertA etc...
....
这是我当前抛出异常的代码:
MRecipeForm parent = new MRecipeForm();
ListViewItem item = new ListViewItem();
item.Tag = parent.recipeListB.Items;
Substrate o = ((ListBox)sender).SelectedItem as Substrate;
double dryWtLbs = o.BatchDryWtLbs; //BatchDryWtLbs is type double
【问题讨论】:
-
如果你使用 Substrate o = ((ListBox)sender).SelectedItem 作为 Substrate;列表框需要包含 Substrate 类型的项目(对象)
-
我已经编辑了你的标题。请看,"Should questions include “tags” in their titles?"