Listbox control in C#

Working with listbox control: 

Listbox allows to choose more than one items also optionally. 

Properties for listbox: 

1)selecteditems 

2)selectedindices 

3)selectionmode:one/none/multisimple/multiextended

 

    In multiextended, cntrl (or) shiftkeys need to be holded down for multiple selection, in case of multisimple, cntrl (or) shift keys are not required

 

Example on listbox: 

Open windows forms application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select windows forms application project 

 

place a listbox with selection mode=multiextended

place a button 

place one more listbox

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms;

 

namespace WindowsFormsApplication8 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        private void Form1_Load(object sender, EventArgs e) 

        { 

            string[] x = { "java", "c#", "c", "c++", "asp", "sap", "jsp" }; 

            //logic 1 for adding above values in listbox1 

            for (int i = 0; i < x.Length; i++) 

                listBox1.Items.Add(x[i]); 

            //logic2 

            listBox1.DataSource = x; 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            //ienumarator is a predefined interface, which is the part of system.collections name space 

            System.Collections.IEnumerator ie; 

            ie = listBox1.SelectedItems.GetEnumerator(); 

 

            while (ie.MoveNext() == true) 

            { 

                listBox2.Items.Add(ie.Current.ToString()); 

            } 

        } 

    } 

}

 

Excute the project F5

 

What is IEnumerator? 

->this is a predefined interface 

->IEnumarator holds a collection of items (values) and also maintains a record pointer(shows the current items) 

->record pointer by defaults points to the before first record 

->MoveNext() moves the record pointer to the next item, If next item is not existing then moveNext() returns false.