GUI/UserRadioGroup.cs
author sl
Thu, 01 Jan 2015 23:35:49 +0100
changeset 405 5715aefd2bcc
parent 298 96263190189a
permissions -rw-r--r--
SharpDisplay: Migrating to new robust client scheme.
     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) 2011 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 UserRadioGroup {
    18     private string name;
    19     private int value;
    20     private MenuItem[] menuItems;
    21     private event EventHandler changed;
    22     private PersistentSettings settings;
    23 
    24     public UserRadioGroup(string name, int value,
    25       MenuItem[] menuItems, PersistentSettings settings) {
    26       this.settings = settings;
    27       this.name = name;
    28       if (name != null)
    29         this.value = settings.GetValue(name, value);
    30       else
    31         this.value = value;
    32       this.menuItems = menuItems;
    33       this.value = Math.Max(Math.Min(this.value, menuItems.Length - 1), 0);
    34 
    35       for (int i = 0; i < this.menuItems.Length; i++) {
    36         this.menuItems[i].Checked = i == this.value;
    37         int index = i;
    38         this.menuItems[i].Click += delegate(object sender, EventArgs e) {
    39           this.Value = index;
    40         };
    41       }      
    42     }
    43 
    44     public int Value {
    45       get { return value; }
    46       set {
    47         if (this.value != value) {
    48           this.value = value;
    49           if (this.name != null)
    50             settings.SetValue(name, value);
    51           for (int i = 0; i < this.menuItems.Length; i++) 
    52             this.menuItems[i].Checked = i == value;
    53           if (changed != null)
    54             changed(this, null);
    55         }
    56       }
    57     }
    58 
    59     public event EventHandler Changed {
    60       add {
    61         changed += value;
    62         if (changed != null)
    63           changed(this, null);
    64       }
    65       remove {
    66         changed -= value;
    67       }
    68     }
    69   }
    70 }