当列表项以与下拉项相同的文本开头时,如何设置组合框默认值*不在下拉列表中*? [英] How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?

查看:18
本文介绍了当列表项以与下拉项相同的文本开头时,如何设置组合框默认值*不在下拉列表中*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 C#,假设您有一个将 DropDownStyle 设置为 DropDown 的 ComboBox(下拉列表中有项目,但用户也可以手动输入值).如何为 ComboBox 设置一个默认值,该值不在下拉列表中的值列表中,但可能选择的文本开头?通常设置 ComboBox.Text 工作正常,但如果下拉列表中有一项开头的文本作为默认文本,它会自动选择列表中以文本开头的第一项.例如:

Using C#, say you have a ComboBox that has a DropDownStyle set to DropDown (there are items in the drop down but the user can manually enter a value as well). How do you set a default value for the ComboBox that is not in the list of values in the drop down, but begins with text from a possible selection? Normally setting ComboBox.Text works fine, but if there is an item in the drop down that begins with the text you want as the default, it automatically selects the first item in the list that starts with the text. For example:

下拉列表中的值:
c:程序文件
c:windows
d:media

默认值分配
myComboBox.Text = "C:";

结果
表单打开时 ComboBox 的初始值为c:program files".

那我做错了什么?如何正确设置不在以可能选择开头的下拉列表中的项目的默认值?

So what am I doing wrong? How do I correctly set a default value of an item not in the drop down list that begins with a possible selection?

推荐答案

我无法重现您所描述的行为.通过 Items 集合添加三个值,然后将初始值设置为c:"(顺便说一下,您在代码示例中省略了 @)工作正常.我的猜测是您的代码中的其他内容是在您设置组合框后设置它的值.

I couldn't repro the behavior you are describing. Adding the three values via the Items collection and then setting the initial value to "c:" (you omitted an @ in your code sample, by the way) worked fine. My guess is that something else in your code is setting the value of the combo box after you set it.

这篇关于当列表项以与下拉项相同的文本开头时,如何设置组合框默认值*不在下拉列表中*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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