Combobox returns 键入值时为 null
Combobox returns null when value is typed
抱歉,如果它有一些明显的解决方案,但我试图解决它几个小时但找不到解决方案。
我在 WindowsFormsApplication
中使用多个 ComboBox
将 id
与 name
联系起来。问题是,当用户 select 组合框列表中的一个项目时,它工作正常,但是当他 types 一个项目时,组合框的 SelectedValue
属性 是 null
.
为了模拟这个问题,我创建了一个带有一个按钮和一个组合框的 from。
在我的实际应用程序中,我使用 sqlserver 数据库中的表中的数据填充组合框,但为了简单起见,我在这里使用 list
:
填充它
public Form1()
{
InitializeComponent();
List<KeyValuePair<short,short>> l = new List<KeyValuePair<short,short>>();
l.Add(new KeyValuePair<short,short>(1,10));
l.Add(new KeyValuePair<short,short>(2,20));
l.Add(new KeyValuePair<short,short>(3,30));
this.comboBox1.DataSource = l;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";
}
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.SelectedValue == null)
MessageBox.Show("NULL");
else
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}
例如,当用户 select 列表中的第二项 (20) 并单击按钮时,消息框会按预期显示 2,但是如果他在组合框中键入数字 20,SelectedValue
是 null
。
这个问题可以通过改变combobox的样式来解决:
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
但它会阻止用户在组合框中输入内容,所以我不得不使用默认值 ComboBoxStyle.DropDown
。
那是因为组合框没有 select 您键入的项目。添加此选项
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
然后它将 select 找到该项目。
默认设置为AutoCompleteMode.None
。
(我认为)这主要是为建议而设计的,但它可以在这里解决你的问题。另外如果你想显示建议:
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
默认设置为 AutoCompleteSource.None
。
您可以选择在 comboBox1.TextChange 事件上使用 EventHandler,这将允许您亲自处理如何将文本翻译成不同的选项。
这可以添加到设计器中(类似于按钮)。
this.comboBox1.TextChanged += new System.EventHandler(this.UpdateValue);
然后根据想要读取结果的方式并查看是否有匹配项,您可以创建一个转换器,使用另一个 key/value,或者您可以像我将向您展示的那样做一个无聊的旧 tryparse。您将需要该列表作为 属性,您可以参考它来查看您是否找到了正确的结果。
private void UpdateValue(object sender, EventArgs e)
{
short result;
if (short.TryParse(comboBox1.Text, out result))
{
var matches = from val in l where val.Value == result select val.Key;
{
foreach (short m in matches)
this.comboBox1.SelectedValue = m;
}
}
}
祝你好运!如果您需要事件处理程序的更多示例,请告诉我。不要忘记投票。
抱歉,如果它有一些明显的解决方案,但我试图解决它几个小时但找不到解决方案。
我在 WindowsFormsApplication
中使用多个 ComboBox
将 id
与 name
联系起来。问题是,当用户 select 组合框列表中的一个项目时,它工作正常,但是当他 types 一个项目时,组合框的 SelectedValue
属性 是 null
.
为了模拟这个问题,我创建了一个带有一个按钮和一个组合框的 from。
在我的实际应用程序中,我使用 sqlserver 数据库中的表中的数据填充组合框,但为了简单起见,我在这里使用 list
:
public Form1()
{
InitializeComponent();
List<KeyValuePair<short,short>> l = new List<KeyValuePair<short,short>>();
l.Add(new KeyValuePair<short,short>(1,10));
l.Add(new KeyValuePair<short,short>(2,20));
l.Add(new KeyValuePair<short,short>(3,30));
this.comboBox1.DataSource = l;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";
}
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.SelectedValue == null)
MessageBox.Show("NULL");
else
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}
例如,当用户 select 列表中的第二项 (20) 并单击按钮时,消息框会按预期显示 2,但是如果他在组合框中键入数字 20,SelectedValue
是 null
。
这个问题可以通过改变combobox的样式来解决:
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
但它会阻止用户在组合框中输入内容,所以我不得不使用默认值 ComboBoxStyle.DropDown
。
那是因为组合框没有 select 您键入的项目。添加此选项
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
然后它将 select 找到该项目。
默认设置为AutoCompleteMode.None
。
(我认为)这主要是为建议而设计的,但它可以在这里解决你的问题。另外如果你想显示建议:
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
默认设置为 AutoCompleteSource.None
。
您可以选择在 comboBox1.TextChange 事件上使用 EventHandler,这将允许您亲自处理如何将文本翻译成不同的选项。
这可以添加到设计器中(类似于按钮)。
this.comboBox1.TextChanged += new System.EventHandler(this.UpdateValue);
然后根据想要读取结果的方式并查看是否有匹配项,您可以创建一个转换器,使用另一个 key/value,或者您可以像我将向您展示的那样做一个无聊的旧 tryparse。您将需要该列表作为 属性,您可以参考它来查看您是否找到了正确的结果。
private void UpdateValue(object sender, EventArgs e)
{
short result;
if (short.TryParse(comboBox1.Text, out result))
{
var matches = from val in l where val.Value == result select val.Key;
{
foreach (short m in matches)
this.comboBox1.SelectedValue = m;
}
}
}
祝你好运!如果您需要事件处理程序的更多示例,请告诉我。不要忘记投票。