一直在寻找一个清晰的例子。 我做了一个新的对象,包括设置几个属性,将整个对象添加到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类型的项目(对象) – Jim
我已编辑过您的标题。请参阅[“应该在其标题中包含”标签“吗?”](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) – Jim