【问题标题】:How to get the ValueMember of an item in a Listbox for a given index number?如何获取给定索引号的列表框中项目的 ValueMember?
【发布时间】:2015-03-14 08:26:43
【问题描述】:

我有一个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` 
}

【问题讨论】:

    标签: c# visual-studio-2010 listbox valuemember


    【解决方案1】:

    您是否尝试过以下代码:

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

    YourItemClassType 是您添加到 ListBox1 的类,YourItemClassType 包含 Number 和 Data 属性

    希望有帮助

    【讨论】:

    • Console.WriteLine 与 ListBox?
    • @TalhaIrfan ConsoleWriteLine 如果您使用 Visual Studio,将打印到您的 OutPut 窗口,也用于测试,您可以在任何位置显示它,例如在消息框中
    • 你是说 Debug.WriteLine 吗?
    • @TalhaIrfan 没关系,我用控制台快速查看输出窗口
    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多