如何获取给定索引号的列表框中项目的 ValueMember?

How to get the ValueMember of an item in a Listbox for a given index number?

我有一个 ListBox,其中包含一个名为 "Data" 的 DisplayMember 和一个名为 "Number" 的 ValueMember。我想使用如下循环获取所有项目的 ValueMember

for (int i = 0; i < ListBox1.Items.Count; i++)
{ 
    //Get the `ValueMember` of `Item` where it's `Index` is `i` 
}

您是否试过如下代码:

for (int i = 0; i < ListBox1.Items.Count; i++)
{ 
   Console.WriteLine((ListBox1.Items[i] as YourItemClassType).Number.ToString());
}

YourItemClassType 是您添加到 ListBox1 的 class,YourItemClassType 包含数字和数据属性

希望有所帮助