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

      <tfoot id='phKy0'></tfoot>

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

        • <bdo id='phKy0'></bdo><ul id='phKy0'></ul>
      1. 将列表中的学生姓名、姓氏和年龄添加到 ListBox

        时间:2023-10-07
          1. <small id='28Y8t'></small><noframes id='28Y8t'>

              <bdo id='28Y8t'></bdo><ul id='28Y8t'></ul>

            • <legend id='28Y8t'><style id='28Y8t'><dir id='28Y8t'><q id='28Y8t'></q></dir></style></legend>
                <tbody id='28Y8t'></tbody>
                <i id='28Y8t'><tr id='28Y8t'><dt id='28Y8t'><q id='28Y8t'><span id='28Y8t'><b id='28Y8t'><form id='28Y8t'><ins id='28Y8t'></ins><ul id='28Y8t'></ul><sub id='28Y8t'></sub></form><legend id='28Y8t'></legend><bdo id='28Y8t'><pre id='28Y8t'><center id='28Y8t'></center></pre></bdo></b><th id='28Y8t'></th></span></q></dt></tr></i><div id='28Y8t'><tfoot id='28Y8t'></tfoot><dl id='28Y8t'><fieldset id='28Y8t'></fieldset></dl></div>
                <tfoot id='28Y8t'></tfoot>

                1. 本文介绍了将列表中的学生姓名、姓氏和年龄添加到 ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 3 个 文本框,我可以在其中输入 NameSurnameAge.在我按下button1后,它会生成一个具有这些属性的新学生.

                  I have 3 textboxes where I can enter Name,Surname and Age. After i press button1, it makes a new student with these attributes.

                  如何将具有所有 3 个属性的学生添加到 ListBox?看起来像这样:

                  How can I add student with all 3 attributes to ListBox? Look like this:

                  /#/-- 姓名 -- 姓 -- 年龄
                  1 -- 约翰 -- 史密斯 -- 21
                  2 --托尼 -- 霍克 -- 22

                  /#/ -- Name -- Surname -- Age
                  1 -- John -- Smith -- 21
                  2 -- Tony -- Hawk -- 22

                  我现在的代码:

                  public class Students
                  {
                       public string Name;
                       public string Surname;
                       public int Age;
                  }
                  public partial class Form1 : Form
                  {
                       List<Students> group = new List<Students>();
                       public Form1()
                       {
                           InitializeComponent();
                       }
                  
                       private void label1_Click(object sender, EventArgs e)
                       {
                           Students student = new Students();
                           student.Name = textBox1.Text;
                           student.Surname = textBox2.Text;
                           student.Age = Convert.ToInt32(textBox3.Text);
                           group.Add(student);
                       }
                  }
                  

                  推荐答案

                  试试这个:

                  listBox1.DataSource = group;
                  listBox1.DisplayMember = "Name"; //Set the DisplayMember property to avoid call ToString()
                  

                  或者这个:

                  foreach (var item in group)
                  {
                      listBox1.Items.Add(item);
                  }
                  listBox1.DisplayMember = "Name";
                  

                  您还应该将类中的字段更改为如下属性:

                  Also you shoud change the fields in your class to properties like this:

                  public class Students
                  {
                      public string Name { get; set; }
                      public string Surname { get; set; }
                      public int Age { get; set; }
                      public override string ToString()
                      {
                          return string.Format("{0} -- {1} -- {2}", Name, Surname, Age);
                      }
                  }
                  

                  我想你想要这个(通过按下按钮它添加到 ListBox 并且你不再需要 group 列表. 只是不要忘记覆盖类中的ToString):

                  I think you want this (By pressing button it adds to the ListBox and you don't need the group list anymore. Just don't forget to override the ToString in the class):

                  private void button1_Click(object sender, EventArgs e)
                  {
                      Students student = new Students();
                      student.Name = textBox4.Text;
                      student.Surname = textBox5.Text;
                      student.Age = Convert.ToInt32(textBox6.Text);
                      listBox1.Items.Add(student);         
                  }
                  

                  这篇关于将列表中的学生姓名、姓氏和年龄添加到 ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 PadLeft 函数在 C# 中同样概述列表框值 下一篇:直接上传新的 Ftp 列表框行

                  相关文章

                2. <tfoot id='eYwtQ'></tfoot>

                      <bdo id='eYwtQ'></bdo><ul id='eYwtQ'></ul>

                    1. <legend id='eYwtQ'><style id='eYwtQ'><dir id='eYwtQ'><q id='eYwtQ'></q></dir></style></legend>

                    2. <small id='eYwtQ'></small><noframes id='eYwtQ'>

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