継承ができない場合の RadioGroupの作成例です。
機能追加したラジオボタン
ラジオボタンにインデックスをオブジェクト値を追加したオブジェクトを作成
/// <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の作成
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) { };
}
}
呼び出し側
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;
}