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

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

问题描述

我有一个窗体上的组合框。 ComboBox的 DropDownStyle 属性设置为下拉,以便用户可以从下拉列表中选择一个项目或手动键入一些文字。

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.

当用户从下拉列表中的项目,我想才出现在组合框的文本字段中做出一些改变,以该项目的文本。要使用非常简单的例子,假设在下拉列表中包含的项目,它由一个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

当选择其中一个项目,我想只出现在组合框的文本字段的描述。因此,当102狗时,字符串狗应显示在文本字段中,准备由用户进行编辑,并在下拉列表中的项目应该是不变的。

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.

我想我能听,说,在 SelectionChangeCommitted 事件的组合框,并设置文本到任何我喜欢的组合框的财产。但是,如果我这样做,我的变化做出文本被忽略,并且整个字符串(102狗)仍显示在组合框。

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,以指示组合框的文本我的设置是不是在下拉列表中的项目。但是,这只是完全清除文本字段,而不管我修改文本属性。

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 是错误的情况下被使用,因为它似乎火得太快,我的目的(即文本属性似乎只的的的 SelectionChangeCommitted 事件处理程序已完成了与我的选择更新)。但是,所有其他的组合框的事件也无法工作,包括的SelectedIndexChanged DropDownClosed

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.

我想这将是pretty的琐碎获得了这幅。还有的必须是一个简单的方法来做到这一点,我敢肯定,我失去了一些东西明显...任何想法?

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天全站免登陆