GUI/UserOption.cs
author moel.mich
Sun, 27 May 2012 14:23:31 +0000
changeset 344 3145aadca3d2
parent 166 fa9dfbfc4145
permissions -rw-r--r--
Changed the license to the Mozilla Public License 2.0 and update the licensing information.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Windows.Forms;
    14 using OpenHardwareMonitor.Utilities;
    15 
    16 namespace OpenHardwareMonitor.GUI {
    17   public class UserOption {
    18     private string name;
    19     private bool value;
    20     private MenuItem menuItem;
    21     private event EventHandler changed;
    22     private PersistentSettings settings;
    23 
    24     public UserOption(string name, bool value,
    25       MenuItem menuItem, PersistentSettings settings) {
    26 
    27       this.settings = settings;
    28       this.name = name;
    29       if (name != null)
    30         this.value = settings.GetValue(name, value);
    31       else
    32         this.value = value;
    33       this.menuItem = menuItem;
    34       this.menuItem.Checked = this.value;
    35       this.menuItem.Click += new EventHandler(menuItem_Click);
    36     }
    37 
    38     private void menuItem_Click(object sender, EventArgs e) {
    39       this.Value = !this.Value;
    40     }    
    41 
    42     public bool Value {
    43       get { return value; }
    44       set {
    45         if (this.value != value) {
    46           this.value = value;
    47           if (this.name != null)
    48             settings.SetValue(name, value);
    49           this.menuItem.Checked = value;
    50           if (changed != null)
    51             changed(this, null);
    52         }
    53       }
    54     }
    55 
    56     public event EventHandler Changed {
    57       add {
    58         changed += value;
    59         if (changed != null)
    60           changed(this, null);
    61       }
    62       remove {
    63         changed -= value;
    64       }
    65     }
    66   }
    67 }