ComboBox.SelectedValue不工作 [英] ComboBox.SelectedValue not working

查看:166
本文介绍了ComboBox.SelectedValue不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有什么问题?

myComboBox.Items.Clear();
myComboBox.Items.AddRange(new string[]{"one","two"});
myComboBox.SelectedValue = "one";

它没有选择任何东西。

推荐答案

如果你像这样填充组合框:

If you populate the combobox like this:

myComboBox.Items.AddRange(new string[]{"one","two"});

您必须使用 ComboBox.SelectedItem ComboBox.SelectedIndex 属性设置/获取所选项:

You must use the ComboBox.SelectedItem or the ComboBox.SelectedIndex property to set/get the selected item:

myComboBox.SelectedItem = "one"; //or
myComboBox.SelectedIndex = 0; 








href =https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvalue%28v=vs.110%29.aspx =nofollow> ComboBox.SelectedValue 属性继承自
ListControl ,且必须使用只有


  • 控制绑定到 DataSource

  • ValueMember DisplayMember 属性。

  • the control is bound to a DataSource
  • and ValueMember and DisplayMember properties are definied.

这篇关于ComboBox.SelectedValue不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆