目的:项目有需要,创建一个支持数据绑定的单选按钮列表。 设想:动态识别多种数据源, 动态生成一定数量的按钮。 准备:创建一个自定义控件,添加一个Panel对象。 优点: 支持多种数据源做绑定,并可以自己添加自定义数据源。 缺点:错误的抛出没有做到位,没有完全做到设计时定义所有数据。原因:懒。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Collections; namespace RVProject { public partial class RadioButtonList : UserControl { Variable#region Variable private Object _dataSource; private ButtonValueMapping[] _mappings; private Object _internalDataSource; private string _displayMember; private string _valueMember; private string _value; #endregion ctor.#region ctor. public RadioButtonList() { InitializeComponent(); } #endregion prop#region prop public System.Object DataSource { get { return _dataSource; } set { _dataSource = value; } } public string DisplayMember { get { return _displayMember; } set { _displayMember = value; } } public string ValueMember { get { return _valueMember; } set { _valueMember = value; } } public string Value { get { return _value; } set { if (Value != null) { _value = value; SetValue(value); } } } #endregion event#region event public delegate void RadioSelectedHandler(object sender, SelectedEventArgs e); public event RadioSelectedHandler RadioItemSeleted; #endregion public method#region public method public void DataBind() { if (_dataSource == null) throw new NullReferenceException("Null reference in Property: DataSource "); PrepareData(); DrawControl(); } #endregion //Internal Function internal function#region internal function void DrawControl() { panel.Controls.Clear(); int count = _mappings.Length; //Draw and set control int height = 0; int x_aris = 10; int y_aris = 10; for (int i = 0; i < count; i++) { //create the radio button RadioButton radio = new RadioButton(); radio.Name = i.ToString(); radio.Text = _mappings[i].Text; radio.AutoSize = true; //put radio button into the panel panel.Controls.Add(radio); radio.Location = new Point(x_aris, y_aris + height); height += radio.Height; //Add click event to radio button radio.Click += new EventHandler(radio_Click); } } //Deal with the data source. Add additional code here if you want some new type of objet to be the datasource. void PrepareData() { //deal with the datasouce if (_dataSource is DataTable) _internalDataSource = ((DataTable)_dataSource).DefaultView; if (_dataSource is DataView) _internalDataSource = _dataSource; //Exception if (_internalDataSource == null) throw new InvalidCastException("The data source is not a desinged type."); //prepare the _radiobutton & _mappings to creat the radio listre DataView & DataTable as data source#region DataView & DataTable as data source if (_internalDataSource is DataView) { int radioCount = ((DataView)_internalDataSource).Count; _mappings = new ButtonValueMapping[radioCount]; try { for (int i = 0; i < radioCount; i++) { //Set index _mappings[i].Index = i; //Set display text _mappings[i].Text = ((DataView)_internalDataSource)[i][_displayMember].ToString(); //Set value if (_valueMember == null || _valueMember == string.Empty) { _mappings[i].Value = i.ToString(); } else { _mappings[i].Value = ((DataView)_internalDataSource)[i][_valueMember].ToString(); } } } catch (Exception e) { throw e; } } #endregion } //internal event when a radio button is clicked. this fuction will call a public event. void radio_Click(object sender, EventArgs e) { _value = _mappings[int.Parse(((RadioButton)sender).Name)].Value; SelectedEventArgs se = new SelectedEventArgs(); se.Value = _mappings[int.Parse(((RadioButton)sender).Name)].Value; RadioItemSeleted(this, se); } //When Value changes , the relative radio button is selected. void SetValue(string value) { if (_mappings == null) throw new NullReferenceException("Data has not bound to the control"); foreach(ButtonValueMapping map in _mappings) { if (map.Value == value) { ((RadioButton)panel.Controls[map.Index.ToString()]).Checked = true; } } } #endregion internal struct ButtonValueMapping { public int Index; public string Value; public string Text; } public class SelectedEventArgs : EventArgs { public string Value; } }}转载于:https://www.cnblogs.com/deltag1984/archive/2008/06/06/1215368.html
