2016-01-13 56 views
-3

我需要将我的列表添加到列表框中。我搜索了本网站上的所有问题,但都没有工作,我总是在列表框中找到像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,但它仍然没有工作。

解决

+0

你应该添加替代的的ToString在你的Speler类 – Steve

+0

@Steve谢谢你的工作,也删除了我的listBox1.DataSource = spelers;并添加了一个foreach循环,我添加了我的列表foreach(Speler alles in spelers) { listBox1.Items.Add(alles); } – RensvDijck

+0

你应该已经发布了Speler类的代码。 –

回答

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 
} 

这样,你总是加入一个新元素相同的列表,而不是每次创建一个新列表。

+0

另外它是明智的记得 – siranen

+0

试过这个,但它说:非可调用成员'ListControl.DataSource'不能用作方法。我已经解决了它btw – RensvDijck

+0

@RensvDijck:我的答案中的代码不会尝试将该成员用作方法。不知道你在做什么,这是不同的... – David

-1

很好的例子是在这里:控制台应用程序

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

}

+0

有一些东西适合一个痛苦的人为的,过度强迫,并且完全不必要的例子包含艾因兰德参考... – David

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;