Thursday 5 April 2012

List Box In Window Form Using C#

 Simple Example Of List Box For Beginners


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 List_BoxAdd_RemoveBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox1.Items.Count > 0)
                {
                    listBox2.Items.Add(listBox1.SelectedItem.ToString());
                    listBox1.Items.Remove(listBox1.SelectedItem);
                }
            }
            catch (Exception exx)
            {
                MessageBox.Show(exx.Message);
            }
            if (listBox1.Items.Count > 0)
            {
                listBox1.SelectedIndex = 0;
                listBox2.SelectedIndex = listBox2.Items.Count - 1;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Text = "";
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if(listBox2.Items.Count>0)
                {
                    listBox1.Items.Add(listBox2.SelectedItem.ToString());
                    listBox2.Items.Remove(listBox2.SelectedItem);
                }
            }
            catch(Exception Exx)
            {
                MessageBox.Show(Exx.Message);
            }
            if(listBox2.Items.Count>0)
            {
                listBox2.SelectedIndex=0;
                listBox2.SelectedIndex=listBox2.Items.Count-1;
            }


        }
    }
}

Screen Shots:-


No comments: