<bdo id='8NJ53'></bdo><ul id='8NJ53'></ul>
      <tfoot id='8NJ53'></tfoot>

      1. <i id='8NJ53'><tr id='8NJ53'><dt id='8NJ53'><q id='8NJ53'><span id='8NJ53'><b id='8NJ53'><form id='8NJ53'><ins id='8NJ53'></ins><ul id='8NJ53'></ul><sub id='8NJ53'></sub></form><legend id='8NJ53'></legend><bdo id='8NJ53'><pre id='8NJ53'><center id='8NJ53'></center></pre></bdo></b><th id='8NJ53'></th></span></q></dt></tr></i><div id='8NJ53'><tfoot id='8NJ53'></tfoot><dl id='8NJ53'><fieldset id='8NJ53'></fieldset></dl></div>

        <small id='8NJ53'></small><noframes id='8NJ53'>

        <legend id='8NJ53'><style id='8NJ53'><dir id='8NJ53'><q id='8NJ53'></q></dir></style></legend>
      2. ListBox 上的项目显示为类名

        时间:2023-10-07

          <tbody id='kPXxX'></tbody>
          • <bdo id='kPXxX'></bdo><ul id='kPXxX'></ul>
          • <tfoot id='kPXxX'></tfoot>

              <small id='kPXxX'></small><noframes id='kPXxX'>

              1. <i id='kPXxX'><tr id='kPXxX'><dt id='kPXxX'><q id='kPXxX'><span id='kPXxX'><b id='kPXxX'><form id='kPXxX'><ins id='kPXxX'></ins><ul id='kPXxX'></ul><sub id='kPXxX'></sub></form><legend id='kPXxX'></legend><bdo id='kPXxX'><pre id='kPXxX'><center id='kPXxX'></center></pre></bdo></b><th id='kPXxX'></th></span></q></dt></tr></i><div id='kPXxX'><tfoot id='kPXxX'></tfoot><dl id='kPXxX'><fieldset id='kPXxX'></fieldset></dl></div>
                  <legend id='kPXxX'><style id='kPXxX'><dir id='kPXxX'><q id='kPXxX'></q></dir></style></legend>
                1. 本文介绍了ListBox 上的项目显示为类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  问题如下:我将 ListBox 与一些自定义类的元素列表连接起来(Listpersons = new List()) 使用 DataSource 属性.当然 ValueMemberDisplayMember 都被分配给这个类的适当属性.当我第一次加载数据时,一切看起来都很好.但是,当我单击某个项目(即第 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.

                  我尝试了重新分配 ValueMemberDisplayMember 的不同组合,以及将 null 分配给列表的 DataSource 属性并重新分配该属性的列表,甚至尝试在解除绑定之前将 -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.在表单的构造函数中:

                  列表<人员>人员 = 新列表<人员>();

                  3.在 button1 点击触发的方法中:

                  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.在按钮 2 上触发的方法中单击:

                  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;
                  

                  当我单击 button1 时,列表框已填满,一切正常.当我选择最后一项(Somebode Else")然后单击按钮 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;
                          }
                      }
                  }
                  

                  重现问题的步骤:

                  1. 运行表单
                  2. 点击按钮1
                  3. 选择列表中的最后一个位置(其他人")
                  4. 点击按钮2

                  如果您在列表中选择John Doe"或Jane Doe",一切正常.当重建列表后所选索引无效时,它似乎崩溃".我想这是一些错误.

                  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.

                  推荐答案

                  当将 DataSource 设置为 null 时,它会清除 DisplayMember 值.所以要解决,你设置一个新的DataSource之后设置它,问题就消失了.

                  When one sets the DataSource to null it clears the DisplayMember value. So to resolve, 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";
                  


                  否则在 Person 类中重写 ToString 方法以确保在 DataMember 为空时显示正确的属性:


                  Otherwise in the Person class override the ToString method to ensure that the proper property will be shown if DataMember is empty:

                  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;
                      }
                  }
                  

                  每当您将列表框数据源设置为 List 时,列表框将自动使用 ToString 方法作为显示.使用 selecteditem 只需将其转换为 Person,(Person)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.

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

                  上一篇:如何删除列表框中的多个选定项? 下一篇:C# 强制 ListBox 更新元素

                  相关文章

                    <legend id='QT3Yz'><style id='QT3Yz'><dir id='QT3Yz'><q id='QT3Yz'></q></dir></style></legend>

                    <small id='QT3Yz'></small><noframes id='QT3Yz'>

                      <tfoot id='QT3Yz'></tfoot>
                      • <bdo id='QT3Yz'></bdo><ul id='QT3Yz'></ul>

                      <i id='QT3Yz'><tr id='QT3Yz'><dt id='QT3Yz'><q id='QT3Yz'><span id='QT3Yz'><b id='QT3Yz'><form id='QT3Yz'><ins id='QT3Yz'></ins><ul id='QT3Yz'></ul><sub id='QT3Yz'></sub></form><legend id='QT3Yz'></legend><bdo id='QT3Yz'><pre id='QT3Yz'><center id='QT3Yz'></center></pre></bdo></b><th id='QT3Yz'></th></span></q></dt></tr></i><div id='QT3Yz'><tfoot id='QT3Yz'></tfoot><dl id='QT3Yz'><fieldset id='QT3Yz'></fieldset></dl></div>