Advertisement
NAK

Persist RadioButton Settings (C#)

NAK
Dec 11th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 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(RadioButton RadioButton in groupBox1.Controls.OfType<RadioButton>())
  25.             {   // add RadioButtonSelected setting to this project
  26.                 BitMask = Properties.Settings.Default.RadioButtonSelected;
  27.                 RadioButton.Tag = BitPosition;
  28.                 RadioButton.Checked = (BitMask & Convert.ToInt32(RadioButton.Tag)) == Convert.ToInt32(RadioButton.Tag);
  29.                 BitPosition *= 2;
  30.             }
  31.         }
  32.  
  33.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  34.         {
  35.             RadioButton checkedRadioButton = groupBox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
  36.  
  37.             BitMask = 0;
  38.             BitMask = BitMask ^ Convert.ToInt32(checkedRadioButton.Tag);
  39.             Properties.Settings.Default.RadioButtonSelected = BitMask;
  40.             Properties.Settings.Default.Save();
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement