[英]ComboBox.SelectedValue is not Working
I have a WinForms application.我有一个 WinForms 应用程序。 I've populated my ComboBox with the following code:
我用以下代码填充了我的 ComboBox:
cboGridSize.Items.Clear();
for (int i = 2; i <= 12; i++)
cboGridSize.Items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.SelectedValue = 4;
However, the last line has absolutely no effect.但是,最后一行完全没有效果。 The ComboBox appears with no items selected.
ComboBox 出现时未选择任何项目。
So I was doing some debugging and noticed some odd things.所以我在做一些调试并注意到一些奇怪的事情。 The following image is from the watch window after setting
cboGridSize.SelectedIndex
to 0.下图来自将
cboGridSize.SelectedIndex
设置为 0 后的观察窗口。
Watch Window http://www.softcircuits.com/Client/debugwin.jpg观看窗口 http://www.softcircuits.com/Client/debugwin.jpg
Even though the SelectedItem
property contains exactly what I would expect, SelectedValue
is still null
.即使
SelectedItem
属性包含我所期望的, SelectedValue
仍然是null
。 Although the documentation for SelectedValue
is pathetic, I understood it would contain the value of the selected item ( SelectedItem
).尽管
SelectedValue
的文档很可悲,但我理解它会包含所选项目 ( SelectedItem
) 的值。 Instead, the two properties seem completely unrelated.相反,这两个属性似乎完全无关。 Can anyone see what I have wrong?
谁能看到我有什么问题?
As you can see, I have the ValueMember
property set.如您所见,我设置了
ValueMember
属性。 And the DropDownStyle
property is set to DropDownList
.并且
DropDownStyle
属性设置为DropDownList
。
EDIT:编辑:
Once Nikolay Khil set me straight on the issue here (why the docs for SelectedValue
don't do that escapes me), I decided to simply write my own code to accomplish the same task.一旦 Nikolay Khil 在这里直接解决了我的问题(为什么
SelectedValue
的文档没有做到这一点让我无法理解),我决定简单地编写自己的代码来完成相同的任务。 I'm posting it here in case anyone is interested.我把它贴在这里以防有人感兴趣。
static class ComboBoxHelper
{
public static void LookupAndSetValue(this ComboBox combobox, object value)
{
if (combobox.Items.Count > 0)
{
for (int i = 0; i < combobox.Items.Count; i++)
{
object item = combobox.Items[i];
object thisValue = item.GetType().GetProperty(combobox.ValueMember).GetValue(item);
if (thisValue != null && thisValue.Equals(value))
{
combobox.SelectedIndex = i;
return;
}
}
// Select first item if requested item was not found
combobox.SelectedIndex = 0;
}
}
}
This is implemented as an extension method so I simply change my original code as follows:这是作为扩展方法实现的,因此我只需将原始代码更改如下:
cboGridSize.Items.Clear();
for (int i = 2; i <= 12; i++)
cboGridSize.Items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.LookupAndSetValue(4);
Both ValueMember
and DisplayMember
properties are used only if DataSource
property is defined.只有在定义了
DataSource
属性时,才使用ValueMember
和DisplayMember
属性。
So, you should re-write your code as follows:因此,您应该按如下方式重新编写代码:
private readonly BindingList<KeyValuePair<string, int>> m_items =
new BindingList<KeyValuePair<string, int>>();
public YourForm()
{
InitializeComponent();
cboGridSize.DisplayMember = "Key";
cboGridSize.ValueMember = "Value";
cboGridSize.DataSource = m_items;
for (int i = 2; i <= 12; i++)
m_items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.SelectedValue = 4;
}
Links:链接:
I know this is an old question but I've just come across this problem myself.我知道这是一个老问题,但我自己刚刚遇到了这个问题。 I solved with the following - it's slightly hacky but it works:
我解决了以下问题 - 它有点hacky但它有效:
if(newVal != null)
{
MyComboBox.SelectedValue = newVal;
}
else
{
MyComboBox.SelectedIndex = 0; // the 'None Selected' item
}
Hope this helps someone.希望这可以帮助某人。
This does not answer the OP however...the ComboBox SelectedValue must be an integer type.但是,这不能回答 OP... ComboBox SelectedValue 必须是整数类型。
If you have a short or byte var that holds the value that will set the SelectedValue, it won't work - you will have null/nothing value.如果您有一个短型或字节型变量来保存将设置 SelectedValue 的值,则它将不起作用 - 您将拥有 null/nothing 值。
Use an integer.使用整数。
可以先设置SelectedValue,再设置Datasource等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.