日韩精品中文字幕一区二区-日韩精品中文字幕一区-日韩精品中文字幕视频-日韩精品在线一区二区三区-第一页在线-第一福利视频

當前位置: 首頁編程開發VC(VC++) → c#(winform)中ComboBox添加Key/Value項、獲取選中項、根據Key、Value設置選中項總結

c#(winform)中ComboBox添加Key/Value項、獲取選中項、根據Key、Value設置選中項總結

更多
WinForm下的ComboBox默認是以多行文本來設定顯示列表的, 這通常不符合大家日常的應用, 

因為大家日常應用通常是鍵/值對的形式去綁定它的.

參考了一些網上的例子,最終寫了一個輔助類用于方便對ComboBox的操作:

用下面這個類的實例作為ComboBox的添加項:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace tp7309.Winform
{
    public class ListItem
    {
        public string Key { get; set; }
        public string Value { get; set; }

        public ListItem(string strKey, string strValue)
        {
            this.Key = strKey;
            this.Value = strValue;
        }
        public override string ToString()
        {
            return this.Key;
        }

        /// <summary>
        /// 根據ListItem中的Value找到特定的ListItem(僅在ComboBox的Item都為ListItem時有效)
        /// </summary>
        /// <param name="cmb">要查找的ComboBox</param>
        /// <param name="strValue">要查找ListItem的Value</param>
        /// <returns>返回傳入的ComboBox中符合條件的第一個ListItem,如果沒有找到則返回null.</returns>
        public static ListItem FindByValue(ComboBox cmb, string strValue)
        {
            foreach (ListItem li in cmb.Items)
            {
                if (li.Value == strValue)
                {
                    return li;
                }
            }
            return null;
        }

        /// <summary>
        /// 根據ListItem中的Key找到特定的ListItem(僅在ComboBox的Item都為ListItem時有效)
        /// </summary>
        /// <param name="cmb">要查找的ComboBox</param>
        /// <param name="strValue">要查找ListItem的Key</param>
        /// <returns>返回傳入的ComboBox中符合條件的第一個ListItem,如果沒有找到則返回null.</returns>
        public static ListItem FindByText(ComboBox cmb, string strText)
        {
            foreach (ListItem li in cmb.Items)
            {
                if (li.Value == strText)
                {
                    return li;
                }
            }
            return null;
        }
    }
}

使用前引入命名空間:tp7309.Winform

添加項:


cmb1.Items.Add(new ListItem("key1", "value1"));
cmb1.Items.Add(new ListItem("key2", "value2"));

獲取選中項:

ListItem li = (ListItem)cmb1.SelectedItem;
ListItem li1 = ListItem.FindByValue(cmb1, "value1");   //根據Key得到選中項
ListItem li2 = ListItem.FindByText(cmb1, "key1");      //根據Value得到選中項
string strKey = li.Key;   //得到選中項Key
string strValue = li.Value;   //得到選中項Value

設置選中項:

cmb1.SelectedIndex = 0;    //根據索引修改選中項
cmb1.SelectedItem = ListItem.FindByValue(cmb1, "value1");   //根據Key得到選中項
cmb1.SelectedItem = ListItem.FindByText(cmb1, "key1");      //根據Value得到選中項

 

 

 

 

熱門評論
最新評論
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數: 0/500 (您的評論需要經過審核才能顯示)
主站蜘蛛池模板: 三级在线免费观看 | 高潮毛片又色又爽免费 | 成人免费激情视频 | 美女黄色在线观看 | 成人欧美一区二区三区黑人免费 | 国产精品一区二区免费 | 偷偷操网站| 久久视频这里只有精品 | 97视频免费| 欧美视频在线播放 | av在线精品 | 久久手机视频 | 欧美国产在线观看 | 欧美视频一二三区 | 中文字幕一区二区三区视频 | 五月婷婷综合网 | 激情91 | 亚洲二区在线 | 人人干人人爱 | 日本一区二区高清 | 久久日韩精品 | 国产黄色片视频 | 中文字幕一区二区在线播放 | 一区二区三区视频在线 | 亚洲国产成人精品女人 | 美女视频福利 | 高清乱码男女免费观看 | 九色在线观看 | 天堂中文字幕免费一区 | www超碰| 国产精品99久久久久久久久 | 一区二区三区影院 | 99精品在线观看 | 久久综合在线 | 成人在线网址 | av手机在线免费观看 | 躁躁躁日日躁 | 性色av一区二区 | 中文精品一区 | 色婷婷狠狠 | 国产一区二区久久 |