Winform动态增加ComboBox后SelectedValue无效的问题

窗体上动态创建ComboBox控件,并且打算初始化时自动选定值为"2“的项,代码如下:

            ComboBox comboBoxCarPlateColor = new ComboBox();
            var listCarPlateColor = new List<KeyValuePair<string, string>>();
            listCarPlateColor.Add(new KeyValuePair<string, string>("1", "蓝色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("2", "黄色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("3", "黑色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("4", "白色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("5", "其它"));
            comboBoxCarPlateColor.DataSource = listCarPlateColor;
            comboBoxCarPlateColor.ValueMember = "key";
            comboBoxCarPlateColor.DisplayMember = "value";
            comboBoxCarPlateColor.SelectedValue = "2";
            this.Controls.Add(comboBoxCarPlateColor);

发现执行后SelectedValue还是为null,创建的ComboBox无法选定2这项,搞了半天,终于在一篇文章里找到原因。

原因是动态的ComboBox要在控件创建的时候,才能确定它的父窗体parent,如果没有parent那么SelectedValue则无效。。。不知道是不是BUG

解决方案是在SelectedValue前,记住一定要在这之前,不能之后,加入所属父窗体的设置即可:

            ComboBox comboBoxCarPlateColor = new ComboBox();
            var listCarPlateColor = new List<KeyValuePair<string, string>>();
            listCarPlateColor.Add(new KeyValuePair<string, string>("1", "蓝色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("2", "黄色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("3", "黑色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("4", "白色"));
            listCarPlateColor.Add(new KeyValuePair<string, string>("5", "其它"));
            comboBoxCarPlateColor.DataSource = listCarPlateColor;
            comboBoxCarPlateColor.ValueMember = "key";
            comboBoxCarPlateColor.DisplayMember = "value";
            comboBoxCarPlateColor.Parent = this;//加入这一句
            comboBoxCarPlateColor.SelectedValue = "2";
            this.Controls.Add(comboBoxCarPlateColor);


参考:

http://blog.163.com/prince.king_521/blog/static/106891204201021735844711/



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在C#的WinForm中,ComboBoxSelectedValue属性被重写,这样即使没有绑定数据源,也可以使用SelectedValue来获取当前选中项的value值。 要让SelectedValue绑定生效,需要注意三个关键属性:ItemsSource、SelectedValue和DisplayMemberPath。你可以使用ItemsSource来绑定数据源,SelectedValue来获取选中项的值,DisplayMemberPath来设置显示的文本。 举个例子,如果你有一个列表tList,其中包含了Test对象,每个对象都有id和name属性。你可以将tList设置为ComboBox的数据源,然后通过设置DisplayMember属性为"name",ValueMember属性为"id"来分别设置显示的文本和值。当你选择了索引为1的项时,通过读取SelectedValue属性,你会发现SelectedValue的值为0x10,即对应的id值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C# winformcombobox和listbox控件SelectedValue属性重写](https://download.csdn.net/download/ziyouli/4086797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【WPF绑定2】 ComboBox SelectedValue复杂数据类型绑定](https://blog.csdn.net/songhuangong123/article/details/128385878)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C# ComboBox SelectedText属性和SelectedValue属性理解](https://blog.csdn.net/weixin_42205680/article/details/128346097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值