Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace MyControl
- {
- public class EnumRadioButtonBridge<T>
- {
- Dictionary<RadioButton, T> _buttons = new Dictionary<RadioButton, T>();
- Type enumType;
- #region ICollection<RadioButton> 実装
- public void Add(RadioButton key, T value)
- {
- Add(new KeyValuePair<RadioButton, T>(key, value));
- }
- public void Add(KeyValuePair<RadioButton, T> pair)
- {
- _buttons[pair.Key] = pair.Value;
- Added(pair.Value, pair.Key);
- }
- public void Clear()
- {
- _buttons.Clear();
- }
- public bool ContainsKey(RadioButton key)
- {
- return _buttons.ContainsKey(key);
- }
- public bool Contains(KeyValuePair<RadioButton, T> pair)
- {
- return false;
- }
- public bool Remove(KeyValuePair<RadioButton, T> pair)
- {
- return _buttons.Remove(pair.Key);
- }
- public int Count
- {
- get
- {
- return _buttons.Count;
- }
- }
- public ICollection<RadioButton> Keys
- {
- get
- {
- return _buttons.Keys;
- }
- }
- public ICollection<T> Values
- {
- get
- {
- return _buttons.Values;
- }
- }
- public IEnumerator<System.Collections.Generic.KeyValuePair<RadioButton, T>> GetEnumerator()
- {
- return _buttons.GetEnumerator();
- //throw new NotImplementedException("GetEnumerator()実装しません。");
- }
- public void CopyTo(KeyValuePair<T, RadioButton>[] pair, int n)
- {
- //_buttons.CopyTo(pair,n);
- }
- public bool Remove(RadioButton key)
- {
- return _buttons.Remove(key);
- }
- public bool IsReadOnly
- {
- get
- {
- //return _buttons.IsReadOnly;
- return true;
- }
- }
- public T this[RadioButton key]
- {
- set { this._buttons[key] = value; }
- get { return this._buttons[key]; }
- }
- #endregion
- #region イベント管理
- public delegate void RadioButtonChange(object sender, RadioButtonChangedEventArgs e);
- public event RadioButtonChange RadioButtonChanged = delegate(object sender, RadioButtonChangedEventArgs e) { };
- public class RadioButtonChangedEventArgs : EventArgs
- {
- public RadioButtonChangedEventArgs()
- {
- }
- public T enumValue;
- public T oldValue;
- }
- private void Added(T enumValue, RadioButton btn)
- {
- enumType = enumValue.GetType();
- btn.CheckedChanged += new EventHandler(btn_CheckedChanged);
- }
- /// <summary>
- /// グループに登録されているラジオボタンが変更されました。
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void btn_CheckedChanged(object sender, EventArgs e)
- {
- try
- {
- if ((sender as RadioButton).Checked)
- {
- RadioButtonChangedEventArgs ee = new RadioButtonChangedEventArgs();
- T enumValue = _buttons[sender as RadioButton];
- ee.enumValue = enumValue;
- ee.oldValue = Value;
- Value = enumValue; // プロパティ更新。
- foreach (RadioButton btn in _buttons.Keys)
- {
- if (!btn.Equals(sender))
- {
- btn.Checked = false;
- }
- }
- RadioButtonChanged(this, ee);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- #endregion
- #region プロパティ
- T _currentValue = default(T);
- public T Value
- {
- get
- {
- return _currentValue;
- }
- set
- {
- //if (((object)_currentValue).Equals(value))
- //{
- // return;
- //}
- _currentValue = value;
- foreach (KeyValuePair< RadioButton,T> btn in _buttons)
- {
- if (((object)btn.Value).Equals(value))
- {
- btn.Key.Checked = true;
- }
- else
- {
- btn.Key.Checked = false;
- }
- }
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement