在C# WinForms应用程序中,如何获取列表框项目的“键”?

8
我正在编写一个WinForms应用程序,在其中用户从列表框中选择一个项目并编辑一些数据,这些数据是相关对象的一部分。然后,将从对象列表中应用编辑到底层文件。
在ASP.Net中,为列表项分配不同于用户看到的显示文本的系统值是微不足道的。在WinForms应用程序中,您必须稍微复杂一些(并且在互联网上不经常有关)地设置每个项目的“Displaymember”和“Valuemember”。
我已经做到了这一点。在调试模式下,我确认每个项目现在都有一个值,即显示成员(用户看到的“友好”字符串)和一个键,即Valuemember,它保存要更新数据的哈希表对象的键。
因此,当用户选择要编辑的字符串时,程序应该将“键”传递给哈希表,取出对象并允许对其进行编辑。
问题是什么?
我无法找到任何明显的方法告诉程序查看项目的Valuemember。我天真地以为它会填充列表框的“SelectedValue”属性,但那太简单了。那么我该如何获取列表项的值?
6个回答

6

我尝试使用SelectedIndexChangedSelectedValueChanged都没有成功 - ListBoxSelectedValue属性总是为空。这让我很惊讶。

作为一个笨拙的解决方法,你可以直接从ListBox中提取对象,使用SelectedIndex

public Form1()
{
    InitializeComponent();

    this.listBox1.DisplayMember = "Name";
    this.listBox1.ValueMember = "ID";

    this.listBox1.Items.Add(new Test(1, "A"));
    this.listBox1.Items.Add(new Test(2, "B"));
    this.listBox1.Items.Add(new Test(3, "C"));
    this.listBox1.Items.Add(new Test(4, "D"));
    this.listBox1.Items.Add(new Test(5, "E"));
}

private void OnSelectedIndexChanged(object sender, EventArgs e)
{
    if(-1 != this.listBox1.SelectedIndex)
    {
        Test t = this.listBox1.Items[this.listBox1.SelectedIndex] as Test;
        if(null != t)
        {
            this.textBox1.Text = t.Name;
        }
    }
}

Test是一个简单的类,有两个属性-IDName)。

看起来应该有更好的方法,但如果没有其他方法,这应该可以工作。


你设置了 DataSource 属性吗? - Phaedrus
我没有这样做,只是想快速组合一个示例。我假设(不正确吗?)无论数据来自何处,SelectedValue都会被设置。 - Andy

5

好的,答案是基于Andy的回答得出的,因此我点赞了那个答案。

但是当我创建了一个小类并尝试将listitem转换为该类时,程序抛出了异常。

异常告诉我,程序无法将DictionaryEntry强制转换为我定义的类型的类。

因此,我删除了代理类,并重新构建了请求:

DictionaryEntry de = (DictionaryEntry)listbox.SelectedItem;<br>
string htKey = de.Key.ToString();

一切都很好。

最终答案竟然是如此简单。感谢Andy的提示。


3

我知道这是一个非常旧的帖子,但是我无法将我的列表框项目转换为字典项。这个解决方案适用于我在.NET 3.5的Windows窗体中使用。

KeyValuePair<int, string> kvp = (KeyValuePair<int, string>)listBoxSystems.SelectedItem;
string szValue = kvp.Value;

2
尝试在ListBox1_SelectedValueChanged事件中获取"ValueMember"。
private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    if (ListBox1.SelectedIndex != -1)
    {
        string orbit = ListBox1.SelectedValue.ToString();
    }
}

ArrayList planets = new ArrayList();
planets.Add(new Planet("Mercury", "1"));
planets.Add(new Planet("Venus", "2"));

//Remember to set the Datasource
ListBox1.DataSource = planets;
//Name and Orbit are properties of the 'Planet' class
ListBox1.DisplayMember = "Name";
ListBox1.ValueMember = "Orbit";

0

一种简单而清晰的方式:

将项目(包括键和值)添加到列表框中:

lsbListBoxName.Items.Insert(0, New ListItem("Item 1", 1))
lsbListBoxName.Items.Insert(0, New ListItem("Item 2", 2))
...


根据用户选择获取项目:

Private Sub lsbListBoxName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lsbListBoxName.SelectedIndexChanged
    Console.WriteLine(TryCast(lsbListBoxName.SelectedItem, ListItem).Text)
    Console.WriteLine(TryCast(lsbListBoxName.SelectedItem, ListItem).Value)
End Sub

lsbListBoxName 是 ListBox 的名称,代码是 VB.NET,您可以使用 此在线工具 转换为 C#


0
我在搜索如何获取ListBox中项的值时,最终来到了这里,然后显而易见的想法浮现在我的脑海中。
秘密在于C#VB和其他语言中的Item方法是一个数组,因此要获取任何项的值,只需编写以下代码:
//Get value of the #1 Item in the ListBox;
ListBox1.Items[1].toString();

要获取所有的项目并将其放入文档或字符串中,只需像这样进行循环:
string Value;
for (int c = 0; c < ListBox1.Items.Count; c++)  {
    Value = Value + ListBox1.Items[c].toString();
}

希望我能帮到你们。这是对你们帖子最简单的回答。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接