C# and listboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GertS
    New Member
    • Aug 2007
    • 2

    C# and listboxes

    Hi
    I'm trying to loop through the items in a listbox (files in a directory).
    I've tried using the code/suggestions from another discusion



    "ListBox items accept objects of any type as elements. Find where the
    elements are being added, and use that class in the foreach. For instance,
    if you do this

    listBox1.Items. Add(new MyCustomClass(" Hi", 3, true));

    then you would do this to get the values

    foreach (MyCustomClass item in listBox1.Items) {
    } "
    ,but keep on getting the following error.

    "Error 1 foreach statement cannot operate on variables of type 'System.Windows .Forms.CheckedL istBox' because 'System.Windows .Forms.CheckedL istBox' does not contain a public definition for 'GetEnumerator' C:\Users\gerts\ Documents\Visua l Studio 2005\Projects\D irTest2\DirTest 2\Form1.cs 341 13 DirTest2
    "
    any suggestions?

    Thanks
    Gert
  • shahjapan
    New Member
    • Apr 2007
    • 63

    #2
    [CODE=c]
    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    namespace WinApp1
    {
    class myClass
    {
    private int dataInt;
    private string dataStr;
    private bool dataBool;

    public myClass(int dataInt, string dataStr, bool dataBool)
    {
    this.dataBool = dataBool;
    this.dataInt = dataInt;
    this.dataStr = dataStr;
    }
    }
    }[/CODE]
    [CODE=c]
    private void button3_Click(o bject sender, EventArgs e)
    {
    for (int x = 0; x <= 10; x++)
    listBox1.Items. Add(new myClass(x,x.ToS tring(),true));
    }

    private void button4_Click(o bject sender, EventArgs e)
    {
    foreach (myClass xx in listBox1.Items)
    MessageBox.Show (xx.ToString()) ;
    }[/CODE]

    Originally posted by GertS
    Hi
    I'm trying to loop through the items in a listbox (files in a directory).
    I've tried using the code/suggestions from another discusion





    ,but keep on getting the following error.



    any suggestions?

    Thanks
    Gert

    Comment

    • GertS
      New Member
      • Aug 2007
      • 2

      #3
      Thanks a lot. That sorted my problem out 100%

      Gert

      Comment

      Working...