C# 组合框上的受限自动完成

C# 组合框上的受限自动完成,c#,.net-3.5,user-interface,combobox,C#,.net 3.5,User Interface,Combobox,我有一个组合框,我不希望用户也添加新数据,但我也希望让他们键入他们想要选择的对象的标题 目前我正在使用此代码: protected virtual void comboBoxAutoComplete_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsControl(e.KeyChar)) { //let it go if it's a control char such as esc

我有一个组合框,我不希望用户也添加新数据,但我也希望让他们键入他们想要选择的对象的标题

目前我正在使用此代码:

    protected virtual void comboBoxAutoComplete_KeyPress(object sender, KeyPressEventArgs e) {
        if (Char.IsControl(e.KeyChar)) {
            //let it go if it's a control char such as escape, tab, backspace, enter...
            return;
        }
        ComboBox box = ((ComboBox)sender);

        //must get the selected portion only. Otherwise, we append the e.KeyChar to the AutoSuggested value (i.e. we'd never get anywhere)
        string nonSelected = box.Text.Substring(0, box.Text.Length - box.SelectionLength);

        string text = nonSelected + e.KeyChar;
        bool matched = false;
        for (int i = 0; i < box.Items.Count; i++) {
            if (((DataRowView)box.Items[i])[box.DisplayMember].ToString().StartsWith(text, true, null)) {
                //box.SelectedItem = box.Items[i];
                matched = true;
                break;
            }
        }

        //toggle the matched bool because if we set handled to true, it precent's input, and we don't want to prevent
        //input if it's matched.
        e.Handled = !matched;
    }
protected virtual void comboBoxAutoComplete\u按键(对象发送器,按键事件参数e){
if(Char.IsControl(e.KeyChar)){
//如果它是一个控制字符,比如转义、制表符、退格、回车,就让它走吧。。。
返回;
}
组合框=((组合框)发送方);
//必须仅获取所选部分。否则,我们会将e.KeyChar附加到自动建议的值(即,我们永远不会得到任何结果)
string nonSelected=box.Text.Substring(0,box.Text.Length-box.SelectionLength);
字符串文本=未选择+e.KeyChar;
布尔匹配=假;
对于(int i=0;i
它适用于任何使用绑定到数据库的数据的组合框,并且不区分大小写。但是,如果用户在错误的情况下输入了一些内容,然后从组合框中跳出,则组合框的选定值仍然是-1(或之前的值)。这不是我想要的行为,我希望它将值设置为当前对用户绑定内容的最佳猜测,即自动完成选项

如果您在for循环中看到注释掉的行,我已经尝试过这个方法。那不行。
它是这样做的:
我有一个值为53的“租金”字段
我键入“r”
我得到的结果是“当前”
combobox.SelectedValue返回-1

当前的功能:
我有一个值为53的“租金”字段
我键入“r”
自动完成提示“租金”
这是正确的值,因此我继续,组合框失去焦点
组合框显示“租金”
combobox.SelectedValue返回值-1

我想要的:
我有一个值为53的“租金”字段
我键入“r”
组合框失去焦点,它填充“租金”(即使它的大小写不正确[已经这样做了])
combobox.SelectedValue现在应该返回53

我认为setting box.SelectedValue可能更好,但我不知道如何做到这一点,至少是以高级抽象的方式,如果我知道组合框是如何使用ValueMemeber和Display member实现的,我会复制它,但我不知道


有人对如何解决此错误有什么建议吗?

可能是找错了方向,但您是否尝试过在组合框上启用自动完成功能

comboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;        

最后一行将限制对列表中项目的输入。

看起来我别无选择,只能在LeaveFocus事件中执行,这将处理问题:

    protected void autocomplete_LeaveFocus(object sender, EventArgs e) {
        ComboBox box = ((ComboBox)sender);
        String selectedValueText = box.Text;

        //search and locate the selected value case insensitivly and set it as the selected value
        for (int i = 0; i < box.Items.Count; i++) {
            if (((DataRowView)box.Items[i])[box.DisplayMember].ToString().Equals(selectedValueText, StringComparison.InvariantCultureIgnoreCase)) {
                box.SelectedItem = box.Items[i];
                break;
            }
        }
    }
protectedvoid autocomplete\u LeaveFocus(对象发送方,事件参数e){
组合框=((组合框)发送方);
字符串selectedValueText=box.Text;
//不敏感地搜索和定位所选值大小写,并将其设置为所选值
对于(int i=0;i
在尝试添加匹配项之前,为什么不清除文本?我不是在尝试添加匹配项。我刚刚更新了问题,所以它有更多的细节。是的,我有。但这些并不能阻止用户输入新选项;将DropDownStyle设置为DropDownList意味着您无法键入。它将带您进入带有该角色的第一个项目,但不再进一步。