ورود

View Full Version : SelectedIndex آیتمهای انتخاب شده در ListBox



Davood_amega
چهارشنبه 14 مهر 1389, 19:15 عصر
سلام
به چه صورت Index آیتمهای انتخاب شده در ListBox را به دست میارید؟
مثلا مقادیر انتخاب شده را می توان با کد زیر به دست آورد .

for (int i = 0; i < lstStuffAll.SelectedItems.Count; i++)
{
DataRowView a = (DataRowView)lstStuffAll.SelectedItems[i];
lstStuffBuyIndex.Items.Add(a.Row[0].ToString());
}اما به دست آوردن Index این آیتمها به چه صورتی است ؟

Davood_amega
چهارشنبه 14 مهر 1389, 21:41 عصر
این کد را پیدا کردم :

foreach (String o in lstStuffBuy.SelectedItems)
{
int a = lstStuffBuy.Items.IndexOf(o);
lstStuffBuy.Items.RemoveAt(a);
}
اما خطای زیر را می دهد .

Collection was modified; enumeration operation may not execute.

Davood_amega
چهارشنبه 14 مهر 1389, 22:08 عصر
سلام با این کد خیلی ضایع درست شد :


int z=lstStuffBuy.SelectedItems.Count;
for (int i = 0; i < z; i++)
{
object o = lstStuffBuy.SelectedItems[0];
int a = lstStuffBuy.Items.IndexOf(o);
lstStuffBuy.Items.RemoveAt(a);
}