内容显示页
 
类别:.Net + C# | 浏览(2834) | 2009-5-10 15:42:29

ComboBox的扩展,添加了只读Value属性,和SelectedByValue及SelectedByText两个方法

方法或属性 介绍
Text 原控件属性
Value 等价于ComboBox绑定状态的SelectedValue,或非绑定状态的:Text
SelectedByValue(object obj) 绑定状态时根据ValueMember选择,非绑定状态时根据SelectedItem选择。精确查找
SelectedByText(object obj) 绑定状态时根据DisplayMember选择,非绑定状态时根据SelectedItem选择。精确查找

 

使用示例:

comboBoxEx1.Text
comboBoxEx1.Value

comboBoxEx1.SelectedByValue("2");
comboBoxEx1.SelectedByText("Name3");

控件主要代码:

using System;
using System.Windows.Forms;
//web:http://www.yongfa365.com/
namespace YongFa365.Controls.ComboBoxEx
{
    public partial class ComboBoxEx : ComboBox
    {
        public ComboBoxEx()
        {
            this.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        public void SelectedByValue(object obj)
        {
            if (this.DataSource == null)
            {
                //非绑定时
                this.SelectedItem = obj;
            }
            else
            {
                object preValue = this.SelectedValue;
                //绑定时直接查找
                this.SelectedValue = obj;
                if (this.SelectedValue == null)
                {
                    //查不到保持原控件值不变
                    this.SelectedValue = preValue;
                }
            }
        }

        public void SelectedByText(object obj)
        {
            if (this.DataSource == null)
            {
                //非绑定时
                this.SelectedItem = obj;
            }
            else
            {
                //绑定时
                this.Text = obj.ToString();
            }
        }

        public string Value
        {
            get
            {
                if (this.DataSource == null)
                {
                    //非绑定时返回Text
                    return this.Text;
                }
                else
                {
                    //绑定时返回SelectedValue
                    return this.SelectedValue.ToString();
                }
            }
        }
    }
}

下载地址:ComboBoxEx.rar


引用本页地址:http://www.yongfa365.com/item/ComboBoxEx.html
 
 
相关链接
 
网友评论:
姓名: 记住我
网址:
邮箱:
内容:
验证码:  验证码图片 看不清? 换张图试试
 
     
 
 
文章分类
 
 
.Net + C#(73)
 
 
ASP+VBS(161)
 
 
 
Linux(10)
 
 
 
web 2.0(26)
 
 
 
 
 
心程(68)
 
生活(97)
 
 
     

Power by :柳永法(yongfa365)'Blog  | 京ICP备07011491号  QQ:64049027  E-mail:64049027@qq.com yongfa365'CodePlex yongfa365'CodeGoogle

申请友情链接 要求:跟本站主题相类似正规网站,双方交换为首页位置

转载请注明来源,以便后人及时得到最新、修正、加强版!!!