一直在尋找一個清晰的例子。 我做了一個新的對象,包括設置幾個屬性,將整個對象添加到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