【问题标题】:How do I get at the listbox item's "key" in c# winforms app?如何在 c# winforms 应用程序中获取列表框项的“键”?
【发布时间】:2010-10-05 04:11:53
【问题描述】:

我正在编写一个 winforms 应用程序,其中用户从列表框中选择一个项目并编辑一些构成关联对象一部分的数据。然后将编辑从对象列表应用到基础文件。

在 ASP.Net 中,为列表项分配与用户看到的显示文本不同的系统值是微不足道的。在 winforms 应用程序中,您必须以稍微复杂的(并且在 Internet 上不经常相关)过程来设置每个项目的“Displaymember”和“Valuemember”。

我已经做到了。在调试模式下,我已经确认每个项目现在都有一个值,它是显示成员(用户看到的“友好”字符串)和一个键,即 valuemember,它保存要更新数据的哈希表对象的键存在。

因此,当用户选择要编辑的字符串时,程序应将“密钥”传递给哈希表,拉出对象并允许对其进行编辑。

捕获?

我看不到任何明显的方式告诉程序查看项目的值成员。我天真地期望它会填充列表框的“SelectedValue”属性,但到目前为止这太简单了。那么我到底是如何获得列表项值的呢?

【问题讨论】:

    标签: c# winforms listbox


    【解决方案1】:

    当我在搜索如何获取 ListBox 中项目的值时,我最终来到了这里,然后我想到了显而易见的事情。
    秘诀在于C#VB 和其他方法中的 Item 方法是一个数组,因此要获取任何 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();
    }
    

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

    【讨论】:

      【解决方案2】:

      简单干净的方法:

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

      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 您可以使用 This Online Tool 转换为 C#

      【讨论】:

        【解决方案3】:

        我知道这是一篇非常古老的帖子,但我无法将我的列表框项目转换为字典项目。此解决方案在 .NET 3.5 for windows 窗体中为我工作。

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

        【讨论】:

          【解决方案4】:

          好的,答案是安迪的回答,因此我赞成这个答案。

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

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

          所以我删除了代理类并重新构建了请求:

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

          一切都很好。

          最后的答案非常简单。感谢安迪的提示。

          【讨论】:

            【解决方案5】:

            尝试从 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";
            

            【讨论】:

              【解决方案6】:

              同时使用 SelectedIndexChangedSelectedValueChanged 对我不起作用 - ListBox's SelectedValue 属性始终为空。这也让我感到惊讶。

              作为一种蹩脚的解决方法,您可以直接使用SelectedIndex 将对象从ListBox 中拉出:

              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 属性?
              • 我没有,但我只是想拼凑一个快速样本。我假设(错误地?)无论数据来自何处,都会设置 SelectedValue。
              猜你喜欢
              • 2018-07-15
              • 2011-11-02
              • 2011-01-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多