Wednesday, 22 January 2014

How to use CheckBox and CheckListBox in Windows Application

How to use CheckBox in Windows Application……………………………

First  we take WindowsFormsApplication and follow below step……………..

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 WindowsFormsApplicationCheckBoxList
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string subject="";

int i = 0;
private void btngetchecked_Click(object sender, EventArgs e)
{

foreach (Control c in this.Controls)
{
if (c is CheckBox)

{
CheckBox cb = (CheckBox)c;

if (cb.Checked)
{
i = 1;
subject += cb.Text + ",";

}

}
}
if (i == 0)
{
MessageBox.Show("Please Select atleast one checkbox");
}
else
{
MessageBox.Show("Subject=" + subject.Remove(subject.Length - 1));


}
}
//using  linq Concept ..................................
private void button1_Click(object sender, EventArgs e)
{
foreach (var cb in this.Controls.OfType<CheckBox>().Where(x => x.Checked))
subject += cb.Text + ",";
MessageBox.Show("Subject=" + subject.Remove(subject.Length - 1));

}
// code for  bind checkbox with ListBox Control in Windows Application
private void btnbindlistbox_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{

if(c.GetType()==typeof(CheckBox))
{
CheckBox cb = (CheckBox)c;

if (cb.Checked)
{

listBox1.Items.Add(cb.Text);

}
}
}
}
}
}

How to use CheckListBox in Windows Application……………………………\

we will work with same  WindowsFormsApplication  but take another  Form and follow   below step……………..

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 WindowsFormsApplicationCheckBoxList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
       static string quli;
        private void button1_Click(object sender, EventArgs e)
        {
            quli = "";
            foreach (Object chk in checkedListBox1.CheckedItems)
            {
                quli += chk.ToString() + ",";
            }

            MessageBox.Show(quli.Remove(quli.Length - 1));
       }
       }
}



0 comments:

Post a Comment