zoukankan      html  css  js  c++  java
  • [.net自定义控件]ComboBox控件重写 之ComboBoxEx

    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");

    控件主要代码:

    1. using System;  
    2. using System.Windows.Forms;  
    3. //web:http://www.yongfa365.com/  
    4. namespace YongFa365.Controls.ComboBoxEx  
    5. {  
    6.     public partial class ComboBoxEx : ComboBox  
    7.     {  
    8.         public ComboBoxEx()  
    9.         {  
    10.             this.DropDownStyle = ComboBoxStyle.DropDownList;  
    11.         }  
    12.   
    13.         public void SelectedByValue(object obj)  
    14.         {  
    15.             if (this.DataSource == null)  
    16.             {  
    17.                 //非绑定时  
    18.                 this.SelectedItem = obj;  
    19.             }  
    20.             else  
    21.             {  
    22.                 object preValue = this.SelectedValue;  
    23.                 //绑定时直接查找  
    24.                 this.SelectedValue = obj;  
    25.                 if (this.SelectedValue == null)  
    26.                 {  
    27.                     //查不到保持原控件值不变  
    28.                     this.SelectedValue = preValue;  
    29.                 }  
    30.             }  
    31.         }  
    32.   
    33.         public void SelectedByText(object obj)  
    34.         {  
    35.             if (this.DataSource == null)  
    36.             {  
    37.                 //非绑定时  
    38.                 this.SelectedItem = obj;  
    39.             }  
    40.             else  
    41.             {  
    42.                 //绑定时  
    43.                 this.Text = obj.ToString();  
    44.             }  
    45.         }  
    46.   
    47.         public string Value  
    48.         {  
    49.             get  
    50.             {  
    51.                 if (this.DataSource == null)  
    52.                 {  
    53.                     //非绑定时返回Text  
    54.                     return this.Text;  
    55.                 }  
    56.                 else  
    57.                 {  
    58.                     //绑定时返回SelectedValue  
    59.                     return this.SelectedValue.ToString();  
    60.                 }  
    61.             }  
    62.         }  
    63.     }  
    64. }  
    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

  • 相关阅读:
    个人作业——软件工程实践总结作业
    团队作业第二次—项目选题报告
    结对第二次—文献摘要热词统计及进阶需求
    结对第一次—原型设计(文献摘要热词统计)
    第一次作业-准备篇
    Java面向对象课程设计——购物车
    第04次作业-树
    第03次作业-栈和队列
    第02次作业-线性表
    01——绪论作业
  • 原文地址:https://www.cnblogs.com/top5/p/1586145.html
Copyright © 2011-2022 走看看