在ListBox中的项目显示为一个类名 [英] Items on ListBox show up as a class name

查看:194
本文介绍了在ListBox中的项目显示为一个类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题如下:连接了的ListBox 用(列表与LT一些自定义类的元素列表;人>的人=新的名单,LT ;人>())使用数据源属性。当然, ValueMember 的DisplayMember 均分配给这个类的相应属性。当我第一次加载数据,一切看起来正常。然而,当我点击一些项目(即第7位,从1开始计数),然后重新生成列表和元素的数量小于7,因此我不能在列表中看到正确的文本。相反,每一个项目显示为一个类名,由命名空间之前

The problem is as follows: I connected the ListBox with a list of elements of some custom class (List<Person> persons = new List<Person>()) using DataSource property. Of course ValueMember and DisplayMember are both assigned to appropriate properties of this class. When I first load data, everything looks ok. However, when I click on some item (i.e. 7th position, counting from 1) and then rebuild the list AND the number of elements is LESS than 7, as a result I can't see the proper texts on the list. Instead, every item shows up as a class name, preceded by the namespace.

在换句话说,而不是名单:

In other words, instead of the list:


  • 李四

  • 李四

  • 有人否则

我看到这一点:


  • MyNamespace.Person

  • MyNamespace.Person

  • MyNamespace.Person

它看起来像这取决于最后的SelectedIndex 。如果不再有与该指数中的项目(有较少的项目),出现问题。

It looks like it depends on last SelectedIndex. If there is no longer an item with that index (there are less items), the problem occurs.

我试过重新分配的不同组合 ValueMember 的DisplayMember ,以及到列表中的数据源财产分配零和重新分配列表此属性,甚至试图之前解除绑定分配-1 的SelectedIndex ,但没有人帮助。

I've tried different combinations of reassigning ValueMember and DisplayMember, as well as assigning null to the DataSource property of the list and reassign the list to this property, even tried to assign -1 to SelectedIndex before unbinding, but none of them helped.

我被要求出示一些代码。我会贴上相关的片段:

I was asked to show some code. I'll paste the relevant fragments:

1。类人:

public class Person
{
    private int id;
    private string name;

    public Person(int m_id, string m_name)
    {
        id = m_id;
        name = m_name;
    }

    public int Id
    {
        get
        {
            return id;
        }
    }

    public string Name
    {
        get
        {
            return name;
        }
    }
}`



2。在形式的构造:

列表<&人GT;人=新的List<&人GT;();

3。在方法上buton1点击触发:

listBox1.DataSource = null;    // this is optional. Commenting this line out doesn't help
persons.Add(new Person(1, "John Doe"));
persons.Add(new Person(2, "Jane Doe"));
persons.Add(new Person(3, "Somebody Else"));
listBox1.ValueMember = "Id";
listBox1.DisplayMember = "Name";
listBox1.DataSource = persons;



4。在方法上buton2点击触发:

listBox1.DataSource = null;    // this is optional. Commenting this line out doesn't help
persons.Add(new Person(1, "Person One"));
persons.Add(new Person(2, "Person Two"));
listBox1.ValueMember = "Id";
listBox1.DisplayMember = "Name";
listBox1.DataSource = persons;

当我点击按钮1,列表框被填充,一切工作正常。当我选择最后一项(Somebode其他),然后clisk按钮2,在列表框中显示2个相同的项目:MyNamespace.Person

When I click button1, the listbox is filled and everything works fine. When I select last item ("Somebode Else") and then clisk button2, the listbox shows 2 identical items: "MyNamespace.Person".

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MyNamespace
{
    public partial class Form1 : Form
    {
        private List<Person> persons = new List<Person>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            persons.Clear();
            persons.Add(new Person(1, "John Doe"));
            persons.Add(new Person(2, "Jane Doe"));
            persons.Add(new Person(1, "Somebody Else"));
            listBox1.DataSource = null;
            listBox1.ValueMember = "Id";
            listBox1.DisplayMember = "Name";
            listBox1.DataSource = persons;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            persons.Clear();
            persons.Add(new Person(1, "Person One"));
            persons.Add(new Person(2, "Person Two"));
            listBox1.DataSource = null;
            listBox1.ValueMember = "Id";
            listBox1.DisplayMember = "Name";
            listBox1.DataSource = persons;
        }
    }

    class Person
    {
        private int id;
        private string name;

        public Person(int m_id, string m_name)
        {
            id = m_id;
            name = m_name;
        }

        public int Id
        {
            get
            {
                return id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public string ToString()
        {
            return id + ". " + name;
        }
    }
}



步骤来重现问题:

Steps to reproduce the problem:


  1. 运行形式

  2. 单击按钮1

  3. 选择名单上的最后一个位置(有人否则)

  4. 单击按钮2

  1. Run the form
  2. Click button1
  3. Select last position on the list ("Somebody Else")
  4. Click button2

如果您选择李四或李四的名单上,一切工作正常。这似乎是崩溃的时候,选择的索引不是重建列表之后有效。我想这是一些bug

If you select "John Doe" or "Jane Doe" on the list, everything works fine. It seems to "crash" when the selected index is not valid after rebuilding the list. I guess it's some bug.

推荐答案

在Person类重写ToString方法:

In the Person class override the ToString method:

public class Person
{
    private int id;
    private string name;

    public Person(int m_id, string m_name)
    {
        id = m_id;
        name = m_name;
    }

    public int Id
    {
        get
        {
            return id;
        }
    }

    public string Name
    {
        get
        {
            return name;
        }
    }
    public override string ToString()
    {
        return name;
    }
}



有了这个,只要你的列表框的数据源设置为列表与LT;人> 列表框会自动使用ToString方法作为显示。使用selectedItem属性仅仅是铸造它作为人的事,(人)listBox1.SelectedItem

With this whenever you set the listbox datasource to a List<Person> the listbox will automatically use the ToString method as the display. Using the selecteditem is simply a matter of casting it as Person, (Person)listBox1.SelectedItem.

做了一些更多的调查,结果发现,当你设置数据源它清除DisplayMemeber值。你设置一个新的数据源,问题消失后,将它设置为

Did some more investigating and found out when you set the datasource to null it cleared the DisplayMemeber value. Set it after you set a new Datasource and the problem disappears.

listBox1.DataSource = null;    // this is optional. Commenting this line out doesn't help
persons.Add(new Person(1, "John Doe"));
persons.Add(new Person(2, "Jane Doe"));
persons.Add(new Person(3, "Somebody Else"));
listBox1.DataSource = persons;
listBox1.DisplayMember = "Name";

这篇关于在ListBox中的项目显示为一个类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆