c# - 为什么设置 comboBox.SelectedIndex 无法更改 ComboBox 的可编辑部分中显示的内容?
问题描述
我有一个 C# Winform 应用程序。它有一个ComboBox
. 我的目标是让在ComboBox
下拉列表中选择的项目出现在键入键ComboBox
时的可编辑部分。delete
例如,如果ComboBox
有项目A
,B
。和C
,然后在加载A
时显示项目。Form
如果我单击下拉菜单,将鼠标悬停在 itemC
上,然后键入delete
密钥,我希望下拉列表被关闭并C
出现在ComboBox
.
事实上,我已经验证我得到了选定的项目文本,但是代码行comboBox.SelectedIndex = comboBox.FindStringExact(selectedItemText);
并没有改变显示在可编辑部分的内容ComboBox
MCVE:
注意:表单有一个名为的组合框combobox
和一个名为的文本框textbox
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;
namespace Winforms_Scratch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//using string collection because I need to simulate what is returned from an Application Settings list
StringCollection computerList = new StringCollection { "C", "B", "A" };
ArrayList.Adapter(computerList).Sort();
comboBox.DataSource = computerList;
comboBox.KeyDown += ComboBox_KeyDown;
computerList = null;
}
private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && comboBox.DroppedDown)
{
ComboBox comboBox = (ComboBox)sender;
//get the text of the item in the dropdown that was selected when the Delete key was pressed
string selectedItemText = comboBox.GetItemText(comboBox.SelectedItem);
//take focus away from the combobox to force it to dismiss the dropdown
this.Focus();
//load selectedItemText into the textbox just so we can verify what it is
textBox.Text = selectedItemText;
//set the comboBox SelectedIndex to the index of the item that matches the
//text of the item in the dropdown that was selected when the Delete key was pressed
comboBox.SelectedIndex = comboBox.FindStringExact(selectedItemText);
comboBox.Refresh();
//Stop all further processing
e.Handled = true;
}
else if (e.KeyCode == Keys.Down && !comboBox.DroppedDown)
{
//If the down arrow is pressed show the dropdown list from the combobox
comboBox.DroppedDown = true;
}
}
}
}
解决方案
我的猜测是组合框失去焦点后的行为会有所不同。无论如何,根据我对您要求的理解,我进行了以下更改并且它有效。this.Focus()
如果有单独的要求将焦点返回到窗口,您可以在设置所选项目后调用。您的 SelectedIndex/FindStringExact 方法与将 SelectedItem 设置为字符串相同。
我摆脱了文本框,因为据我所知,这只是为了调试目的。
private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && comboBox.DroppedDown)
{
ComboBox comboBox = (ComboBox)sender;
// Get the text of the item in the dropdown that was selected when the
// Delete key was pressed
string selectedItemText = comboBox.GetItemText(comboBox.SelectedItem);
comboBox.DroppedDown = false;
comboBox.SelectedItem = selectedItemText;
//Stop all further processing
e.Handled = true;
}
else if (e.KeyCode == Keys.Down && !comboBox.DroppedDown)
{
// If the down arrow is pressed show the dropdown list from the combobox
comboBox.DroppedDown = true;
}
}
推荐阅读
- java - How to identify a zip compressed file in a Oracle-Database with Java
- javascript - 在 React 中使用 CSS 模块的问题
- android - 为什么我的 valueformatter 上的索引从 -1 开始?
- google-app-engine - 权限被拒绝 appengine/api/remote_socket/_remote_socket.py
- java - Java 8 Streams API 和堆栈类
- google-chrome - 为什么它显示“访问网站”而不是添加扩展程序?
- javascript - 从对象数组中获取每个名称和 ID
- css - 我无法让导航栏中的导航项目按预期运行
- eclipse - 为什么 STS 需要这么长时间来更新 Maven 依赖项?
- r - 如何生成 n 个马尔可夫链序列,每个序列有 25 个转换