将列表逐个添加到列表框中

时间:2016-01-13 14:00:00

标签: c#

我需要将我的列表添加到列表框中。我搜索了这个网站上的所有问题,但没有工作我总是在我的列表框中得到像listbox1.spelers这样的东西。 这是我现在的代码。

    private void btnAdd_Click(object sender, EventArgs e)
    {
        Speler speler1 = new Speler(tbNaam.Text, tbAge.Text);
        List<Speler> spelers = new List<Speler>();
        spelers.Add(speler1);
        listBox1.DataSource = spelers;
    }

还尝试使用ToArray,但它仍然没有用。

解决

3 个答案:

答案 0 :(得分:2)

您每次都会将控件重新绑定到一个元素的列表中。所以控件只有一个元素。

将列表保持在更高的范围内。例如,如果此类在内存中持久存在(即不是Web应用程序),则将其设为类级别成员:

private List<Speler> spelers = new List<Speler>();

private void btnAdd_Click(object sender, EventArgs e)
{
    Speler speler1 = new Speler(tbNaam.Text, tbAge.Text);
    spelers.Add(speler1);
    listBox1.DataSource = spelers;
    // maybe call listBox1.DataBind() here?  it's been a while since I've had to use forms
}

这样你总是会在同一个列表中添加另一个元素,而不是每次都创建一个新列表。

答案 1 :(得分:0)

如果您使用的是Windows窗体应用程序,则可以使用BindingDource:

Speler speler1 = new Speler(tbNaam.Text, tbAge.Text);
List<Speler> spelers = new List<Speler>();
spelers.Add(speler1);

var bs = new BindingSource();
bs.DataSource = spelers;

listBox1.DataSource = bs;

答案 2 :(得分:-1)

这里有一个很好的例子:Console App

using System;

namespace Enumeration
 {
     using System;
     using System.Collections;

 // implements IEnumerable
 class ListBoxTest : IEnumerable
 {
     private string[] strings;
     private int ctr = 0;

     // private nested implementation of ListBoxEnumerator
     private class ListBoxEnumerator : IEnumerator
     {
         // member fields of the nested ListBoxEnumerator class
         private ListBoxTest currentListBox;
         private int index;

         // public within the private implementation
         // thus, private within ListBoxTest
         public ListBoxEnumerator(ListBoxTest currentListBox)
         {
             // a particular ListBoxTest instance is
             // passed in, hold a reference to it
             // in the member variable currentListBox.
             this.currentListBox = currentListBox;
             index = -1;
         }

         // Increment the index and make sure the
         // value is valid
         public bool MoveNext()
         {
             index++;
             if (index >= currentListBox.strings.Length)
                 return false;
             else
                 return true;
         }

         public void Reset()
         {
             index = -1;
         }

         // Current property defined as the
         // last string added to the listbox
         public object Current
         {
             get
             {
                 return(currentListBox[index]);
             }
         }
     }  // end nested class

     // Enumerable classes can return an enumerator
     public IEnumerator GetEnumerator()
     {
         return (IEnumerator) new ListBoxEnumerator(this);
     }

     // initialize the listbox with strings
     public ListBoxTest(params string[] initialStrings)
     {
         // allocate space for the strings
         strings = new String[8];

         // copy the strings passed in to the constructor
         foreach (string s in initialStrings)
         {
             strings[ctr++] = s;
         }
     }

     // add a single string to the end of the listbox
     public void Add(string theString)
     {
         strings[ctr] = theString;
         ctr++;
     }

     // allow array-like access
     public string this[int index]
     {
         get
         {
             if (index < 0 || index >= strings.Length)
             {
                 // handle bad index
             }
             return strings[index];
         }
         set
         {
             strings[index] = value;
         }
     }

     // publish how many strings you hold
     public int GetNumEntries()
     {
         return ctr;
     }

 }

public class EnumerationTester
{
   public void Run()
   {
       // create a new listbox and initialize
       ListBoxTest currentListBox =
           new ListBoxTest("Hello", "World");

       // add a few strings
       currentListBox.Add("Who");
       currentListBox.Add("Is");
       currentListBox.Add("John");
       currentListBox.Add("Galt");

       // test the access
       string subst = "Universe";
       currentListBox[1] = subst;

       // access all the strings
       foreach (string s in currentListBox)
       {
           Console.WriteLine("Value: {0}", s);
       }
   }

   [STAThread]
   static void Main()
   {
      EnumerationTester t = new EnumerationTester();
      t.Run();
   }
}

}