This sample code demonstates the working of ‘check all’ and ‘check none’ checkbox behaviours in a Windows Forms application


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void CheckCheck(object sender, EventArgs e)
{
bool flag = true;
foreach (Control ctrl in Form1.ActiveForm.Controls)
{
if (ctrl is CheckBox && ctrl.Name != "chkNone" && ctrl.Name != "chkAll")
{
CheckBox chk = (CheckBox)ctrl;
if (chk.Checked) flag = false;
}
}
chkNone.Checked = flag;
}

private void chkNone_MouseClick(object sender, MouseEventArgs e)
{
CheckAll(false);
}

private void chkAll_MouseClick(object sender, MouseEventArgs e)
{
CheckAll(true);
}
private void CheckAll(bool b)
{
foreach (Control ctrl in Form1.ActiveForm.Controls)
{
if (ctrl is CheckBox && ctrl.Name != "chkNone" && ctrl.Name != "chkAll")
{
CheckBox chk = (CheckBox)ctrl;
chk.Checked = b;
}
}
}

}
}

Notes:

CheckCheck() method must be attached to CheckedChanged event of ALL the checkboxes except “None” and “All”.
chkNone_MouseClick() method is for the MouseClick event of “None” checkbox.
chkAll_MouseClick() method is for the MouseClick event of “All” checkbox.

VN:F [1.1.6_502]
Rating: 0.0/5 (0 votes cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Monitor your filesystem for changes in real time in .NET
  2. Move a control with mouse on form
  3. WinForms – Print using printDialog and printDocument
  4. Find your lucky number in c#
  5. Filter Listbox based on Textbox entry