daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest May 16th, 2018 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private void ColorBtn_Click(object sender, EventArgs e)
  2. {
  3.     var btn = sender as Button;
  4.     var color = btn?.Name == "RedBtn" ? Color.Red : Color.Green;
  5.  
  6.     Controls.OfType<CheckBox>()
  7.         .Where(x=>x.Checked)
  8.         .ToList()
  9.         .ForEach(x=>x.ForeColor = color);
  10. }
  11.    
  12. public partial class Form1 : Form
  13. {
  14.     public Form1()
  15.     {
  16.         InitializeComponent();
  17.  
  18.         ReadColorCheckBoxes();
  19.     }
  20.  
  21.     private void buttonGreen_Click(object sender, EventArgs e)
  22.     {
  23.         SetBackColorCheckBoxes(Color.Green);
  24.         SaveColorCheckBoxes();
  25.     }
  26.  
  27.     private void buttonRed_Click(object sender, EventArgs e)
  28.     {
  29.         SetBackColorCheckBoxes(Color.Red);
  30.         SaveColorCheckBoxes();
  31.     }
  32.  
  33.     private void SetBackColorCheckBoxes(Color color)
  34.     {
  35.         var checkboxes = GetCheckedCheckBoxes();
  36.  
  37.         foreach (var checkbox in checkboxes)
  38.         {
  39.             checkbox.BackColor = color;
  40.         }
  41.     }
  42.  
  43.     private void SaveColorCheckBoxes()
  44.     {
  45.         var checkboxes = GetCheckedCheckBoxes();
  46.  
  47.         foreach (var checkbox in checkboxes)
  48.         {
  49.             Properties.Settings.Default[checkbox.Name] = checkbox.BackColor;
  50.         }
  51.  
  52.         Properties.Settings.Default.Save();
  53.     }
  54.  
  55.     private void ReadColorCheckBoxes()
  56.     {
  57.         foreach (var checkbox in this.Controls.OfType<CheckBox>())
  58.         {
  59.             checkbox.BackColor = (Color)Properties.Settings.Default[checkbox.Name];
  60.         }
  61.     }
  62.  
  63.     private IEnumerable<CheckBox> GetCheckedCheckBoxes()
  64.     {
  65.         return this.Controls
  66.                             .OfType<CheckBox>()
  67.                             .Where(c => c.Checked == true);
  68.     }
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top