在组合框中设置所选项目 - vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19786889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:16:59  来源:igfitidea点击:

set selected item in combobox - vb.net

vb.nettextcomboboxselecteditem

提问by sharkyenergy

I am using this code to add a value to a combobox different then the one displayed: how to add value to combobox item

我正在使用此代码向与显示的组合框不同的组合框添加一个值:如何向组合框项目添加值

Lets suppose i have 3 values in my Combobox:

假设我的组合框中有 3 个值:

 item 1
 item 2
 item 3

If i chose item 2from the dropdown the code in the link works. But if i TYPE item 2manually it doesnt work because i think that typing it only sets the combobox1.textvalue and not the combobox1.selecteditem. I can type a value present in the dropdown, or one not present. If i type one that is present, then the selectedItem property should also be set to the proper value. can this be done?

如果我item 2从下拉列表中选择,则链接中的代码有效。但是如果我item 2手动输入它不起作用,因为我认为输入它只会设置combobox1.text值而不是combobox1.selecteditem. 我可以在下拉列表中输入一个值,也可以输入一个不存在的值。如果我输入一个存在的值,则 selectedItem 属性也应设置为正确的值。可以这样做吗?

Thanks

谢谢

回答by sharkyenergy

solved this way:

这样解决:

Private Sub ComboBox1_Keyup(sender As Object, 
  e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

      ComboBox1.SelectedIndex = ComboBox1.FindStringExact(ComboBox1.Text)

End Sub