从下拉列表中选择项目时更改组合框的文本字段? [英] Change the ComboBox's text field when an item is selected from the drop down list?

查看:20
本文介绍了从下拉列表中选择项目时更改组合框的文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个 ComboBox.ComboBox 的DropDownStyle 属性设置为DropDown,以便用户可以从下拉列表中选择一个项目或手动输入一些文本.

I have a ComboBox on a form. The DropDownStyle property of the ComboBox is set to DropDown, so that the user can select an item from the drop down list or type in some text manually.

当用户从下拉列表中选择一个项目时,我想在项目的文本出现在 ComboBox 的文本字段中之前对其进行一些更改.举一个非常简单的例子,假设下拉列表包含由 ID 和描述组成的项目,如下所示:

When the user selects an item from the drop down list, I'd like to make some changes to the item's text before it appears in the ComboBox's text field. To use a very simplified example, let's say the drop down list contains items that consist of an ID and a description, like so:

101 Cat
102 Dog
103 Bird

When one of these items is selected, I'd like only the description to appear in the ComboBox's text field.So when "102 Dog" is selected, the string "Dog" should be displayed in the text field, ready to be edited by the user, and the items in the drop down list should be unchanged.

When one of these items is selected, I'd like only the description to appear in the ComboBox's text field. So when "102 Dog" is selected, the string "Dog" should be displayed in the text field, ready to be edited by the user, and the items in the drop down list should be unchanged.

我想我可以只收听 ComboBox 的 SelectionChangeCommitted 事件,然后将 ComboBox 的 Text 属性设置为我喜欢的任何值.但是如果我这样做,我对 Text 所做的更改将被忽略,并且整个字符串(102 Dog")仍会显示在 ComboBox 中.

I thought I could just listen to, say, the SelectionChangeCommitted event of the ComboBox, and set the Text property of the ComboBox to whatever I like. But if I do this, the changes I make to Text are ignored, and the entire string ("102 Dog") is still displayed in the ComboBox.

所以我想我也应该将 SelectedIndex 字段更新为 -1,以向 ComboBox 表明我设置的 Text 不是下拉列表.但这只是完全清除文本字段,无论我将 Text 属性更改为什么.

So then I thought I should also update the SelectedIndex field to -1, to indicate to the ComboBox that the Text I'm setting is not an item in the drop down list. But this just clears the text field completely, regardless of what I change the Text property to.

然后我认为 SelectionChangedCommitted 是错误的事件使用,因为它似乎为我的目的太早触发(Text 属性似乎只更新与我的选择 after SelectionChangeCommitted 事件处理程序已完成).但是所有其他 ComboBox 事件也无法工作,包括 SelectedIndexChangedDropDownClosed.

So then I figured that SelectionChangedCommitted is the wrong event to be using, as it appears to fire too soon for my purposes (the Text property seems to only be updated with my selection after the SelectionChangeCommitted event handler has completed). But all other ComboBox events also fail to work, including SelectedIndexChanged and DropDownClosed.

我认为这很容易实现.必须有一个简单的方法来做到这一点,我确定我遗漏了一些明显的东西......有什么想法吗?

I thought this would be pretty trivial to acheive. There's got to be a simple way to do this, and I'm sure I'm missing something obvious... any ideas?

推荐答案

你可以试试这个:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (comboBox1.SelectedIndex > -1)
  {
    string value = comboBox1.Items[comboBox1.SelectedIndex].ToString().Substring(4);
    this.BeginInvoke((MethodInvoker)delegate { this.comboBox1.Text = value; });
  }
}

这篇关于从下拉列表中选择项目时更改组合框的文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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