Thursday, 4 April 2013

Example:ComboBox, ListBox and CheckBox List control




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

        private void Form5_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("AP");
            listBox1.Items.Add("Orrisa");
            listBox1.Items.Add("Maharashtra");
            listBox1.Items.Add("MP");
            listBox1.Items.Add("UP");
            string[]cities={"Bengaluru","Chennai","Delhi","Hyderabad","Kolkata","Mumbai"};
            checkedListBox1.Items.AddRange(cities);
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
                if (comboBox1.FindStringExact(comboBox1.Text) == -1)
                    comboBox1.Items.Add(comboBox1.Text);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.Text);
            MessageBox.Show(comboBox1.SelectedItem.ToString());
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (object obj in listBox1.SelectedItems)
                MessageBox.Show(obj.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str = null;
            foreach (object obj in checkedListBox1.CheckedItems)
                str += obj.ToString() + ",";
            str=str.Substring(0,str.Length-2);
            int pos=str.LastIndexOf(",");
            if(pos!=-1)
            {
                str=str.Remove(pos,1);
                str=str.Insert(pos,"and");
            }
            MessageBox.Show(str);
        }
       
    }
}

No comments:

Post a Comment