惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - Leonny

IIS站点/虚拟目录中访问共享目录(UNC)以及建立后的应用程序的信任级别问题 谁说用Azure就一定要Vista/Win7?Window2003/xp下安装 Windows Azure Tools for Microsoft Visual Studio IE的window.showModalDialog出现内存不足的问题 - Leonny - 博客园 关于清理JBoss服务器端jsp文件缓存的问题 提取assemblyinfo.cs文件里的guid值 经常坐电脑旁的人必喝的健康饮品 Firefox图片模糊的问题 MS SQL Server2005如何得到记录的行号 [原创]OWC生成Excel的效能优化 [译]NUnitForms官方说明文档 C#取得MS Word的总页数 VS2008中文版安装ASP.NET MVC Beta 成功申请了GAE Vista删你文件没商量 3大数据库的变量参数符号 新立得软件包管理器出错 Repeater中點擊按鈕事件時要注意頁面PostBack的問題 關於Web Service 中使用NHibernate時無法序列化IList的問題 如何取得輸入字符串的寬度
WinForm下ComboBox添加项与设定预选项
Leonny · 2008-10-06 · via 博客园 - Leonny

WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用,

因为大家日常应用通常是键/值对的形式去绑定它的.

那么用键值对的形式如何做?

因为Combox的每一个项的值是一个object, 实际上就是一个键/值对.
我用的是下面这个类的实例作为它的一个项:

    /// <summary>
    
/// ComboBox的项
    
/// </summary>
    class ListItem : System.Object
    {
        
private string m_sValue = string.Empty;
        
private string m_sText = string.Empty;/// <summary>
        
/// 值
        
/// </summary>
        public string Value
        {
            
get { return this.m_sValue; }
        }
        
/// <summary>
        
/// 显示的文本
        
/// </summary>
        public string Text
        {
            
get { return this.m_sText; }
        }
public ListItem(string value, string text)
        {
            
this.m_sValue = value;
            
this.m_sText = text;
        }
        
public override string ToString()
        {
            
return this.m_sText;
        }
        
public override bool Equals(System.Object obj)
        {
            
if (this.GetType().Equals(obj.GetType()))
            {
                ListItem that 
= (ListItem)obj;
                
return (this.m_sText.Equals(that.Value));
            }
            
return false;
        }
        
public override int GetHashCode()
        {
            
return this.m_sValue.GetHashCode(); ;
        }

    }

 通过这个类就可以定义ComboBox的值了, 首先我们定义一个ListItem的清单作为ComboBox的数据源:

            List<ListItem> items = new List<ListItem>();
            items.Add(
new ListItem("0""Item_0_Text"));
            items.Add(
new ListItem("1""Item_1_Text"));
            items.Add(
new ListItem("2""Item_2_Text"));
            items.Add(
new ListItem("3""Item_3_Text"));
            items.Add(
new ListItem("4""Item_4_Text"));
            items.Add(
new ListItem("5""Item_5_Text"));

 然后进行相应的设置:

            //将数据源的属性与ComboBox的属性对应
            drpTest.DisplayMember = "Text";        //显示
            drpTest.ValueMember = "Value";        //

然后进就可以进行绑定了:

            drpTest.DataSource = items;        //绑定数据

绑定数据之后, 就可以对其进行默认选择项的设置, 取值等操作:

            drpTest.SelectedValue = "4";        //设定选择项

            
//取得当前选择的项
            ListItem selectedItem = (ListItem)drpTest.SelectedItem;
            
string value = selectedItem.Value;    //
            string text = selectedItem.Text;    //显示的文字

其他操作大家就依样画葫芦吧. 呵呵.