目的:項目有需要,建立一個支援資料綁定的單選按鈕清單。
設想:動态識别多種資料源, 動态生成一定數量的按鈕。
準備:建立一個自定義控件,添加一個Panel對象。
優點: 支援多種資料源做綁定,并可以自己添加自定義資料源。
缺點:錯誤的抛出沒有做到位,沒有完全做到設計時定義所有資料。原因:懶。
實作RadioButtonList自定義WinForm控件 using System;
實作RadioButtonList自定義WinForm控件 using System.Collections.Generic;
實作RadioButtonList自定義WinForm控件 using System.ComponentModel;
實作RadioButtonList自定義WinForm控件 using System.Drawing;
實作RadioButtonList自定義WinForm控件 using System.Data;
實作RadioButtonList自定義WinForm控件 using System.Text;
實作RadioButtonList自定義WinForm控件 using System.Windows.Forms;
實作RadioButtonList自定義WinForm控件 using System.Collections;
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 namespace RVProject
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 public partial class RadioButtonList : UserControl
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 Variable#region Variable
實作RadioButtonList自定義WinForm控件 private Object _dataSource;
實作RadioButtonList自定義WinForm控件 private ButtonValueMapping[] _mappings;
實作RadioButtonList自定義WinForm控件 private Object _internalDataSource;
實作RadioButtonList自定義WinForm控件 private string _displayMember;
實作RadioButtonList自定義WinForm控件 private string _valueMember;
實作RadioButtonList自定義WinForm控件 private string _value;
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 ctor.#region ctor.
實作RadioButtonList自定義WinForm控件 public RadioButtonList()
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 InitializeComponent();
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 prop#region prop
實作RadioButtonList自定義WinForm控件 public System.Object DataSource
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 get
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 return _dataSource;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 set
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _dataSource = value;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 public string DisplayMember
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 get
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 return _displayMember;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 set
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _displayMember = value;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 public string ValueMember
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 get
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 return _valueMember;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 set
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _valueMember = value;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 public string Value
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 get
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 return _value;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 set
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 if (Value != null)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _value = value;
實作RadioButtonList自定義WinForm控件 SetValue(value);
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 event#region event
實作RadioButtonList自定義WinForm控件 public delegate void RadioSelectedHandler(object sender, SelectedEventArgs e);
實作RadioButtonList自定義WinForm控件 public event RadioSelectedHandler RadioItemSeleted;
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 public method#region public method
實作RadioButtonList自定義WinForm控件 public void DataBind()
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 if (_dataSource == null)
實作RadioButtonList自定義WinForm控件 throw new NullReferenceException("Null reference in Property: DataSource ");
實作RadioButtonList自定義WinForm控件 PrepareData();
實作RadioButtonList自定義WinForm控件 DrawControl();
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 //Internal Function
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 internal function#region internal function
實作RadioButtonList自定義WinForm控件 void DrawControl()
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 panel.Controls.Clear();
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 int count = _mappings.Length;
實作RadioButtonList自定義WinForm控件 //Draw and set control
實作RadioButtonList自定義WinForm控件 int height = 0;
實作RadioButtonList自定義WinForm控件 int x_aris = 10;
實作RadioButtonList自定義WinForm控件 int y_aris = 10;
實作RadioButtonList自定義WinForm控件 for (int i = 0; i < count; i++)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 //create the radio button
實作RadioButtonList自定義WinForm控件 RadioButton radio = new RadioButton();
實作RadioButtonList自定義WinForm控件 radio.Name = i.ToString();
實作RadioButtonList自定義WinForm控件 radio.Text = _mappings[i].Text;
實作RadioButtonList自定義WinForm控件 radio.AutoSize = true;
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 //put radio button into the panel
實作RadioButtonList自定義WinForm控件 panel.Controls.Add(radio);
實作RadioButtonList自定義WinForm控件 radio.Location = new Point(x_aris, y_aris + height);
實作RadioButtonList自定義WinForm控件 height += radio.Height;
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 //Add click event to radio button
實作RadioButtonList自定義WinForm控件 radio.Click += new EventHandler(radio_Click);
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 //Deal with the data source. Add additional code here if you want some new type of objet to be the datasource.
實作RadioButtonList自定義WinForm控件 void PrepareData()
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 //deal with the datasouce
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 if (_dataSource is DataTable)
實作RadioButtonList自定義WinForm控件 _internalDataSource = ((DataTable)_dataSource).DefaultView;
實作RadioButtonList自定義WinForm控件 if (_dataSource is DataView)
實作RadioButtonList自定義WinForm控件 _internalDataSource = _dataSource;
實作RadioButtonList自定義WinForm控件 //Exception
實作RadioButtonList自定義WinForm控件 if (_internalDataSource == null) throw new InvalidCastException("The data source is not a desinged type.");
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 //prepare the _radiobutton & _mappings to creat the radio listre
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 DataView & DataTable as data source#region DataView & DataTable as data source
實作RadioButtonList自定義WinForm控件 if (_internalDataSource is DataView)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 int radioCount = ((DataView)_internalDataSource).Count;
實作RadioButtonList自定義WinForm控件 _mappings = new ButtonValueMapping[radioCount];
實作RadioButtonList自定義WinForm控件 try
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 for (int i = 0; i < radioCount; i++)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 //Set index
實作RadioButtonList自定義WinForm控件 _mappings[i].Index = i;
實作RadioButtonList自定義WinForm控件 //Set display text
實作RadioButtonList自定義WinForm控件 _mappings[i].Text = ((DataView)_internalDataSource)[i][_displayMember].ToString();
實作RadioButtonList自定義WinForm控件 //Set value
實作RadioButtonList自定義WinForm控件 if (_valueMember == null || _valueMember == string.Empty)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _mappings[i].Value = i.ToString();
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 else
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _mappings[i].Value = ((DataView)_internalDataSource)[i][_valueMember].ToString();
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 catch (Exception e)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 throw e;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 //internal event when a radio button is clicked. this fuction will call a public event.
實作RadioButtonList自定義WinForm控件 void radio_Click(object sender, EventArgs e)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 _value = _mappings[int.Parse(((RadioButton)sender).Name)].Value;
實作RadioButtonList自定義WinForm控件 SelectedEventArgs se = new SelectedEventArgs();
實作RadioButtonList自定義WinForm控件 se.Value = _mappings[int.Parse(((RadioButton)sender).Name)].Value;
實作RadioButtonList自定義WinForm控件 RadioItemSeleted(this, se);
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 //When Value changes , the relative radio button is selected.
實作RadioButtonList自定義WinForm控件 void SetValue(string value)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 if (_mappings == null)
實作RadioButtonList自定義WinForm控件 throw new NullReferenceException("Data has not bound to the control");
實作RadioButtonList自定義WinForm控件 foreach(ButtonValueMapping map in _mappings)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 if (map.Value == value)
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 ((RadioButton)panel.Controls[map.Index.ToString()]).Checked = true;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 #endregion
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 internal struct ButtonValueMapping
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 public int Index;
實作RadioButtonList自定義WinForm控件 public string Value;
實作RadioButtonList自定義WinForm控件 public string Text;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 public class SelectedEventArgs : EventArgs
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件
實作RadioButtonList自定義WinForm控件 {
實作RadioButtonList自定義WinForm控件 public string Value;
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
實作RadioButtonList自定義WinForm控件 }
轉載于:https://www.cnblogs.com/deltag1984/archive/2008/06/06/1215368.html