WinForm自定义RadionButtonList控件

2015-12-13 11:29:26  访问(3767) 赞(0) 踩(0)


相关下载:SlowX.DevExPressEkRadionButtonList[code]  SlowX.DevExPressEkRadionButtonList[release]  百度网盘     



  •  

  • // 控件源码 //
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Data;
    using System.Reflection;
    using System.ComponentModel;
    
    namespace SlowX.DevExPressEkRadionButtonList.Controls
    {
        /// <summary>
        /// RadionButtonList
        /// </summary>
        public class EkRadionButtonList
            :
            Panel
        {
            /// <summary>
            /// 
            /// </summary>
            public EkRadionButtonList()
            {
                this.Height = 20;
            }
    
    
            /// <summary>
            /// Fires when Slider position has changed
            /// </summary>
            [Description("Event fires when the Value property changes")]
            [Category("Action")]
            public event EventHandler EkRadioSelectedIndexChanged;
    
            #region 属性
    
            #region DataTextField ~ 获取或设置为列表项提供文本内容的数据源字段
    
            /// <summary>
            /// DataTextField ~ 获取或设置为列表项提供文本内容的数据源字段
            /// </summary>
            protected string m_DataTextField = null;
    
            /// <summary>
            /// DataTextField ~ 获取或设置为列表项提供文本内容的数据源字段
            /// </summary>
            public string DataTextField
            {
                get
                {
                    if (m_DataTextField == null || m_DataTextField.Length == 0)
                        return "TheName";
    
                    return m_DataTextField;
                }
                set
                {
                    m_DataTextField = value;
                }
            }
    
            #endregion DataTextField ~ 获取或设置为列表项提供文本内容的数据源字段
    
    
            #region DataValueField ~ 获取或设置为各列表项提供值的数据源字段
    
            /// <summary>
            /// DataValueField ~ 获取或设置为各列表项提供值的数据源字段
            /// </summary>
            protected string m_DataValueField = null;
    
            /// <summary>
            /// DataValueField ~ 获取或设置为各列表项提供值的数据源字段
            /// </summary>
            public string DataValueField
            {
                get
                {
                    if (m_DataValueField == null || m_DataValueField.Length == 0)
                        return "ID";
    
                    return m_DataValueField;
                }
                set
                {
                    m_DataValueField = value;
                }
            }
    
            #endregion DataValueField ~ 获取或设置为各列表项提供值的数据源字段
    
    
    
            /// <summary>
            /// 位置中的Y
            /// </summary>
            protected int m_LocY = 2;
    
            /// <summary>
            /// 位置中的Y
            /// </summary>
            public int LocY
            {
                get
                {
                    return m_LocY;
                }
                set
                {
                    m_LocY = value;
                }
            }
    
    
            /// <summary>
            /// 位置中的StartX
            /// </summary>
            protected int m_LocStartX = 0;
    
            /// <summary>
            /// 位置中的StartX
            /// </summary>
            public int LocStartX
            {
                get
                {
                    return m_LocStartX;
                }
                set
                {
                    m_LocStartX = value;
                }
            }
    
    
            /// <summary>
            /// 位置中的Space
            /// </summary>
            protected int m_LocSpace = 5;
    
            /// <summary>
            /// 位置中的Space
            /// </summary>
            public int LocSpace
            {
                get
                {
                    return m_LocSpace;
                }
                set
                {
                    m_LocSpace = value;
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool IsCheck()
            {
                RadioButton rdb = SelectedItemGet();
    
                if (rdb == null)
                    return false;
    
                return true;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dt"></param>
            public void DataBindCtrl(DataTable dt)
            {
                RadioButton rdb = null;
                int x = LocStartX;
                int y = LocY; 
    
                int iCount = dt.Rows.Count;
                DataRow dr = null;
    
                string strDataTextField = DataTextField;
    
                if (strDataTextField == null || strDataTextField.Length == 0)
                    strDataTextField = "TheName";
    
                for (int i = 0; i < iCount; ++i)
                {
                    dr = dt.Rows[i];
    
                    rdb = new RadioButton();
                    rdb.Name = this.Name + "_" + i.ToString();
                    rdb.Text = dr[strDataTextField].ToString();
                    rdb.Tag = dr;
                    rdb.CheckedChanged+=new EventHandler(rdb_CheckedChanged);
    
                    rdb.Location = new System.Drawing.Point(x, y); 
                    rdb.AutoSize = true;
                    rdb.Cursor = System.Windows.Forms.Cursors.Hand;
    
                    this.Controls.Add(rdb);
    
                    x = rdb.Size.Width + x + LocSpace;
    
                }
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void rdb_CheckedChanged(object sender, EventArgs e)
            {
                if (EkRadioSelectedIndexChanged != null)
                {
                    RadioButton rdb = sender as RadioButton;
    
                    if (rdb == null)
                        return;
    
                    if(rdb.Checked)
                        EkRadioSelectedIndexChanged(this, new EventArgs());
                }
            }
    
            #endregion 属性
    
            /// <summary>
            /// 计算控件的长度
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            protected int GetCtrlSizeLen(string str)
            {
                int iLen = GetLength(str);
    
                return iLen * 5 + 20;
            }
    
            /// <SUMMARY>
            /// 获得字符串长度[中文长度]
            /// </SUMMARY>
            /// <PARAM name="str"></PARAM>
            /// <RETURNS></RETURNS>
            private  int GetLength(string str)
            {
                if (str == null || str.Length == 0)
                    return 0;
    
                ASCIIEncoding ascii = new ASCIIEncoding();
    
                int tempLen = 0;
    
                byte[] s = ascii.GetBytes(str);
    
                int iLen = s.Length;
    
                for (int i = 0; i < iLen; i++)
                {
                    if ((int)s[i] == 63)
                    {
                        tempLen += 2;
                    }
                    else
                    {
                        tempLen += 1;
                    }
                }
    
                return tempLen;
            }
    
    
            /// <summary>
            /// 获得选中的元素
            /// </summary>
            public virtual object SelectedTagGet()
            {
                RadioButton rdb = SelectedItemGet();
    
                if (rdb == null)
                    return null;
    
                return rdb.Tag;
            }
    
            /// <summary>
            /// 
            /// </summary>
            public virtual int SelectedIndexGet()
            {
                RadioButton rdb = SelectedItemGet();
    
                if (rdb == null)
                    return -1;
    
                return int.Parse(rdb.Name.Substring(this.Name.Length + 1));
            }
    
          
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theValue"></param>
            public virtual void SelectedIndexSet(int theValue)
            {
                if (theValue == -1)
                {
    
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:theValue == -1"
                        );
                }
    
    
                string findName = this.Name + "_" + theValue.ToString();
    
                foreach (Control c in this.Controls)
                {
                    if (c == null)
                        continue;
    
                    if (!(c is RadioButton))
                        continue;
    
                    if (c.Name == findName)
                    {
                        (c as RadioButton).Checked = true;
                        return;
                    }
                }
    
    
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:索引int theValue[" + theValue.ToString() + "]越界"
                    );
            }
    
            /// <summary>
            /// 获得选中的项
            /// </summary>
            /// <returns></returns>
            public virtual RadioButton SelectedItemGet()
            {
                RadioButton theResult = null;
    
                string preName = this.Name + "_";
                foreach (Control c in this.Controls)
                {
                    if (c == null)
                        continue;
    
                    if (!(c is RadioButton))
                        continue;
    
                    if (c.Name == null || c.Name.Length == 0)
                        continue;
    
                    if (!c.Name.StartsWith(preName))
                        continue;
    
    
                    theResult= c as RadioButton;
    
                    if (theResult.Checked)
                        return theResult;
                }
    
                return null;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public virtual int SelectedIntValueGet()
            {
                string str = SelectedValueGet();
    
                if (str == null)
                {
    
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:没有获得选中记录"
                        );
                }
    
                return int.Parse(str);
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public virtual long SelectedLongValueGet()
            {
                string str = SelectedValueGet();
    
                if (str == null)
                {
    
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:没有获得选中记录"
                        );
                }
    
                return long.Parse(str);
            }
    
    
            /// <summary>
            /// 获得选中
            /// </summary>
            /// <returns></returns>
            public virtual string SelectedValueGet()
            {
                RadioButton rdb = SelectedItemGet();
    
                if (rdb == null)
                    return null;
    
    
                string strDataValueField = DataValueField;
    
                if (strDataValueField == null || strDataValueField.Length == 0)
                    strDataValueField = "ID";
    
                return (rdb.Tag as DataRow)[strDataValueField].ToString();
    
            }
    
            /// <summary>
            /// 获得选中
            /// </summary>
            /// <returns></returns>
            public virtual string SelectedTextGet()
            {
                RadioButton rdb = SelectedItemGet();
    
                if (rdb == null)
                    return null;
    
                return rdb.Text;
    
            }
    
             
            /// <summary>
            /// 设置选中
            /// </summary>
            /// <param name="theValue"></param>
            public virtual void SelectedIntValueSet(int theValue)
            {
                SelectedValueSet(theValue.ToString());
            }
    
            /// <summary>
            /// 设置选中
            /// </summary>
            /// <param name="theValue"></param>
            public virtual void SelectedValueSet(string theValue)
            {
    
                string strDataValueField = DataValueField;
    
                if (strDataValueField == null || strDataValueField.Length == 0)
                    strDataValueField = "ID";
    
                RadioButton theResult = null;
    
                string preName = this.Name + "_";
                foreach (Control c in this.Controls)
                {
                    if (c == null)
                        continue;
    
                    if (!(c is RadioButton))
                        continue;
    
                    if (c.Name == null || c.Name.Length == 0)
                        continue;
    
                    if (!c.Name.StartsWith(preName))
                        continue;
    
                    theResult = c as RadioButton;
    
                    if ((theResult.Tag as DataRow)[strDataValueField].ToString() == theValue)
                    {
                        theResult.Checked = true;
                        return;
                    }
                }
            }
    
            /// <summary>
            /// 设置选中
            /// </summary>
            /// <param name="theValue"></param>
            public virtual void SelectedTextSet(string theValue)
            {
                RadioButton theResult = null;
    
                string preName = this.Name + "_";
                foreach (Control c in this.Controls)
                {
                    if (c == null)
                        continue;
    
                    if (!(c is RadioButton))
                        continue;
    
                    if (c.Name == null || c.Name.Length == 0)
                        continue;
    
                    if (!c.Name.StartsWith(preName))
                        continue;
    
                    theResult = c as RadioButton;
    
                    if(theResult.Text == theValue)
                    {
                        theResult.Checked = true;
                        return;
                    }
                }
            }
     
        }
    }
    
    
    
  • 
    
    // 调用源码 //
    
    
            /// <summary>
            /// 构建缺省的DataSet数据源
            /// </summary>
            /// <returns></returns>
            protected DataSet DataSetBuild()
            {
                DataSet theResult = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("TheName");
    
                int idx = 1;
                DataRow dr = null;
    
                dr = dt.NewRow();
                dr["ID"] = idx++;
                dr["TheName"] = "北京";
                dt.Rows.Add(dr);
    
    
                dr = dt.NewRow();
                dr["ID"] = idx++;
                dr["TheName"] = "天津";
                dt.Rows.Add(dr);
    
    
                dr = dt.NewRow();
                dr["ID"] = idx++;
                dr["TheName"] = "上海";
                dt.Rows.Add(dr);
    
    
                dr = dt.NewRow();
                dr["ID"] = idx++;
                dr["TheName"] = "重庆";
                dt.Rows.Add(dr);
    
                theResult.Tables.Add(dt);
    
                return theResult;
    
            }
    
            private void toolStripButton_绑定_Click(object sender, EventArgs e)
            {
                try
                {
                    DataSet ds =DataSetBuild();
    
                    ekRadionButtonList_City.DataTextField = "TheName";
                    ekRadionButtonList_City.DataValueField = "ID";
                    ekRadionButtonList_City.DataBindCtrl(ds.Tables[0]);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void ekRadionButtonList_City_EkRadioSelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    RadioButton rdb = ekRadionButtonList_City.SelectedItemGet();
    
                    if (rdb == null)
                    {
                        MessageBox.Show("没有选中记录。");
                        return;
                    }
    
                    label_选中城市.Text = rdb.Text;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButton_取值_Click(object sender, EventArgs e)
            {
                try
                {
                    string strValue = ekRadionButtonList_City.SelectedValueGet();
    
                    MessageBox.Show(strValue);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
    

标签:WinForm自定义RadionButtonList控件 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)