请问:在windows 应用程序中,用c#语言,怎么取到ListBox 中的所有选项?

解决方案 »

  1.   

    根据Items.Count来遍历所有Items[i]呗
      

  2.   

    2楼的,为啥用foreach而不用for啊?
      

  3.   

    请各位看清楚是:windows 应用程序中,不是asp.net!
      

  4.   


    啥意思? winForm不支持foreach?你想取得所有选项目的是干嘛? 如果只是"取"的话,那ListBox.Items就是你所要的了如果是取来用的话,那就行用foreach或forfor(int i=0;i<ListBox.Items.Count;i++)
    {
        ListBox.Items[i].......
    }
      

  5.   


    呵呵WinForm和WebForm的ListBox确实不一样的。
    WebForm中,ListBox.Items是ListItemCollection,每一个Item都必须是ListItem对象;而winform则不同,System.Windows.Forms.ListBox.Items的类型是ObjectCollection,这意味着可以在Items中放入任何对象。当然,就楼主的问题,取所有Item的方法都是一样的:for(int i=0; i<listBox.Items.Count; i++)
    {
        listBox.Items[i].....
    }或者foreach(ListItem li in listBox.Items) // Web form
    // foreach(object obj in listBox.Items) // winform
    {
        li......
    }
      

  6.   

    借贵宝地询问一下,此种应用下.for和foreach那个性能更好一些.