継承ができない場合の RadioGroupの作成例です。
機能追加したラジオボタン
ラジオボタンにインデックスをオブジェクト値を追加したオブジェクトを作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/// <summary> /// 機能追加したラジオボタン /// </summary> class RadioButtonUp { public RadioButtonUp(RadioButton rbtn) { _RadioButton = rbtn; this.Index = -1; this.Value = null; } public RadioButton Button { get { return _RadioButton; } } RadioButton _RadioButton = null; public int Index { set { _Index = value; } get { return _Index; } } private int _Index; public object Value { set { _Value = value; } get { return _Value; } } private object _Value; } |
RadioGroupの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
namespace RadioGroupSample { delegate void RadioGroup_onClick_Delegate(EventArgs e); class RadioGroup { Hashtable _ht = new Hashtable(); GroupBox _GroupBox = null; /// <summary> /// コンストラクタ /// </summary> /// <param name="sender">グループボックス</param> public RadioGroup(GroupBox sender) { System.Collections.ArrayList al = new System.Collections.ArrayList(); _GroupBox = sender; //ラジオボタンの名前を取得し並び替える foreach (RadioButton rbtn in sender.Controls) { al.Add(rbtn.Name); rbtn.Click += new EventHandler(this._onClick); } al.Sort(); //ラジオボタン設定 int index = 0; foreach (string name in al) { RadioButton rb = (RadioButton)_GroupBox.Controls[name]; RadioButtonUp rbup = new RadioButtonUp(rb); rbup.Index = index; _ht.Add(name, rbup); index++; } } /// <summary> /// インデックスの取得及び設定をします。 /// </summary> public int ItemIndex { set { foreach (DictionaryEntry de in _ht) { RadioButtonUp rb = (RadioButtonUp)de.Value; if (rb.Index == value) { rb.Button.Checked = true; } } } get { int index = -1; foreach (DictionaryEntry de in _ht) { RadioButtonUp rb = (RadioButtonUp)de.Value; if (rb.Button.Checked) { index = rb.Index; break; } } return index; } } /// <summary> /// 現在のインデックス位置の値を取得する。 /// </summary> public object ItemValue { get { object val = null; foreach (DictionaryEntry de in _ht) { RadioButtonUp rb = (RadioButtonUp)de.Value; if (rb.Button.Checked) { val = rb.Value; break; } } return val; } } /// <summary> /// ボタン名毎に値を設定します。 /// </summary> /// <param name="name"></param> /// <param name="val"></param> /// <returns></returns> public bool setValue(string name, object val) { bool bc = false; if (_ht.ContainsKey(name)) { RadioButtonUp rb = (RadioButtonUp)_ht[name]; rb.Value = val; bc = true; } return bc; } /// <summary> /// クリックイベント /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _onClick(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb.Checked) { onClick(e); } } public event RadioGroup_onClick_Delegate onClick = delegate (EventArgs e) { }; } } |
呼び出し側
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private void Form1_Load(object sender, EventArgs e) { _radioGroup1 = new RadioGroup(groupBox1); _radioGroup1.onClick += RadioGroup1_onClick; _radioGroup1.setValue("radioButton1", 10); _radioGroup1.setValue("radioButton2", 20); _radioGroup1.setValue("radioButton3", 30); } RadioGroup _radioGroup1 = null; private void button1_Click(object sender, EventArgs e) { int no = _radioGroup1.ItemIndex; int v = (int)_radioGroup1.ItemValue; } public void RadioGroup1_onClick(EventArgs e) { int no = _radioGroup1.ItemIndex; } |