Advertisement
NAK

Persist CheckBox Settings (C#)

NAK
Dec 11th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private int BitPosition = 1;
  15.         private int BitMask = 0;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             foreach(CheckBox CheckBox in groupBox1.Controls.OfType<CheckBox>())
  25.             {   // add CheckBoxSelected setting to this project
  26.                 BitMask = Properties.Settings.Default.CheckBoxSelected;
  27.                 CheckBox.Tag = BitPosition;
  28.                 CheckBox.Checked = (BitMask & Convert.ToInt32(CheckBox.Tag)) == Convert.ToInt32(CheckBox.Tag);
  29.                 CheckBox.CheckedChanged += CheckBox_CheckedChanged;
  30.                 BitPosition *= 2;
  31.             }
  32.         }
  33.  
  34.         private void CheckBox_CheckedChanged(object sender, EventArgs e)
  35.         {
  36.             BitMask = BitMask ^ Convert.ToInt32(((System.Windows.Forms.Control)(sender)).Tag);
  37.         }
  38.  
  39.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  40.         {
  41.             Properties.Settings.Default.CheckBoxSelected = BitMask;
  42.             Properties.Settings.Default.Save();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement