Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace MyControl
  11. {
  12. public class EnumRadioButtonBridge<T>
  13. {
  14. Dictionary<RadioButton, T> _buttons = new Dictionary<RadioButton, T>();
  15. Type enumType;
  16.  
  17. #region ICollection<RadioButton> 実装
  18.  
  19. public void Add(RadioButton key, T value)
  20. {
  21. Add(new KeyValuePair<RadioButton, T>(key, value));
  22. }
  23.  
  24. public void Add(KeyValuePair<RadioButton, T> pair)
  25. {
  26. _buttons[pair.Key] = pair.Value;
  27. Added(pair.Value, pair.Key);
  28. }
  29.  
  30. public void Clear()
  31. {
  32. _buttons.Clear();
  33. }
  34.  
  35. public bool ContainsKey(RadioButton key)
  36. {
  37. return _buttons.ContainsKey(key);
  38. }
  39.  
  40. public bool Contains(KeyValuePair<RadioButton, T> pair)
  41. {
  42. return false;
  43. }
  44.  
  45. public bool Remove(KeyValuePair<RadioButton, T> pair)
  46. {
  47. return _buttons.Remove(pair.Key);
  48. }
  49.  
  50. public int Count
  51. {
  52. get
  53. {
  54. return _buttons.Count;
  55. }
  56. }
  57.  
  58. public ICollection<RadioButton> Keys
  59. {
  60. get
  61. {
  62. return _buttons.Keys;
  63. }
  64. }
  65.  
  66. public ICollection<T> Values
  67. {
  68. get
  69. {
  70. return _buttons.Values;
  71. }
  72. }
  73.  
  74. public IEnumerator<System.Collections.Generic.KeyValuePair<RadioButton, T>> GetEnumerator()
  75. {
  76. return _buttons.GetEnumerator();
  77. //throw new NotImplementedException("GetEnumerator()実装しません。");
  78. }
  79.  
  80. public void CopyTo(KeyValuePair<T, RadioButton>[] pair, int n)
  81. {
  82. //_buttons.CopyTo(pair,n);
  83. }
  84.  
  85. public bool Remove(RadioButton key)
  86. {
  87. return _buttons.Remove(key);
  88. }
  89.  
  90. public bool IsReadOnly
  91. {
  92. get
  93. {
  94. //return _buttons.IsReadOnly;
  95. return true;
  96. }
  97. }
  98.  
  99. public T this[RadioButton key]
  100. {
  101. set { this._buttons[key] = value; }
  102. get { return this._buttons[key]; }
  103. }
  104.  
  105. #endregion
  106.  
  107. #region イベント管理
  108. public delegate void RadioButtonChange(object sender, RadioButtonChangedEventArgs e);
  109. public event RadioButtonChange RadioButtonChanged = delegate(object sender, RadioButtonChangedEventArgs e) { };
  110. public class RadioButtonChangedEventArgs : EventArgs
  111. {
  112. public RadioButtonChangedEventArgs()
  113. {
  114. }
  115. public T enumValue;
  116. public T oldValue;
  117. }
  118. private void Added(T enumValue, RadioButton btn)
  119. {
  120. enumType = enumValue.GetType();
  121. btn.CheckedChanged += new EventHandler(btn_CheckedChanged);
  122. }
  123. /// <summary>
  124. /// グループに登録されているラジオボタンが変更されました。
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. void btn_CheckedChanged(object sender, EventArgs e)
  129. {
  130. try
  131. {
  132. if ((sender as RadioButton).Checked)
  133. {
  134. RadioButtonChangedEventArgs ee = new RadioButtonChangedEventArgs();
  135. T enumValue = _buttons[sender as RadioButton];
  136.  
  137. ee.enumValue = enumValue;
  138. ee.oldValue = Value;
  139. Value = enumValue; // プロパティ更新。
  140.  
  141.  
  142. foreach (RadioButton btn in _buttons.Keys)
  143. {
  144. if (!btn.Equals(sender))
  145. {
  146. btn.Checked = false;
  147. }
  148. }
  149.  
  150.  
  151. RadioButtonChanged(this, ee);
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. MessageBox.Show(ex.ToString());
  157. }
  158. }
  159. #endregion
  160.  
  161. #region プロパティ
  162. T _currentValue = default(T);
  163. public T Value
  164. {
  165. get
  166. {
  167. return _currentValue;
  168. }
  169. set
  170. {
  171. //if (((object)_currentValue).Equals(value))
  172. //{
  173. // return;
  174. //}
  175. _currentValue = value;
  176. foreach (KeyValuePair< RadioButton,T> btn in _buttons)
  177. {
  178. if (((object)btn.Value).Equals(value))
  179. {
  180. btn.Key.Checked = true;
  181. }
  182. else
  183. {
  184. btn.Key.Checked = false;
  185. }
  186. }
  187. }
  188. }
  189. #endregion
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement