winform:自定义RadioButtonList:EkRadionButtonList

2017-06-12 15:19:25  访问(1921) 赞(0) 踩(0)

EkRadionButtonList


using System;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.ComponentModel;

namespace SlowX.CreateLibApp.DevExPress.Controls
{
    /// <summary>
    /// 自定义RadioButtonList
    /// </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 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 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;
                }
            }
        }
 
    }
}


应用代码



// 绑定 //
  
            ekRadionButtonList_EmCodeModel.DataBindCtrl
                (
                    SlowX.CreateLibApp.Enums.CodeModel.ReturnDataTable()
                );

            ekRadionButtonList_EmCodeModel.SelectedIndexSet(0);

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)