C#combobox只允许列表项但允许输入 [英] C# combobox only allow list items BUT allow typing

查看:115
本文介绍了C#combobox只允许列表项但允许输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个通过数据集填充的组合框。那一切都很好。我将'AutoCompleteMode'设置为'SuggestAppend'并将'AutoCompleteSource'设置为'ListItems'。这一切都很好并且有效。问题是,用户仍然可以输入他想要的任何旧文本。我希望当前行为(建议/追加)与用户能够输入内容,但我不希望他们能够输入列表项目中不存在的任何内容。



我想我可以处理一个'焦点'事件,但有更好的方法吗?????



干杯,

Brad

Hello,

I have a combobox that is populated via a dataset. That's all fine. I have 'AutoCompleteMode' set to 'SuggestAppend' and 'AutoCompleteSource' set to 'ListItems'. That's all fine and works. The problem is, that the user can still enter any old text he wants. I want the current behaviour (suggest/append) with the user being able to type in stuff, but I don't want them to be able to enter anything that ISN'T in the list items.

I figure I could handle one of the 'Focus' events, but is there a better way?????

Cheers,
Brad

推荐答案

看看ComboBox.TextUpdate

也许这可以帮助

ComboBox.TextUpdate
Take a look at ComboBox.TextUpdate
Perhaps this can help
ComboBox.TextUpdate


我没有相应的docco,但是有一个样式类型的属性,其名称类似于DropDownList,它禁用了输入的条目,但仍然允许自动完成查找。

祝你好运,

Peter
I don't have the appropriate docco to hand, but there is a style-type property with a name like "DropDownList" which disables typed entry, but still allows "autocomplete" lookup.
Good luck,
Peter


你好,



请试试这个解决方案。它适用于我。



在你的组合框中



1.设置这些属性

AutoCompleteMode = SuggestAppend

AutoCompleteSource =无

DropDownStyle = ComboBoxStyle.DropDown;



2 。添加KeyPress事件句柄并写下这个

Hello,

Please try this solution. It works in my side.

In you combo box

1. set these properties
AutoCompleteMode = SuggestAppend
AutoCompleteSource = None
DropDownStyle = ComboBoxStyle.DropDown;

2. Add KeyPress event handle and write this
public virtual void Cbo_KeyPress(object sender, KeyPressEventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    cb.DroppedDown = true;
    string strFindStr = "";
    if (e.KeyChar == (char)8)
    {
        if (cb.SelectionStart <= 1)
        {
            cb.Text = "";
            return;
        }

        if (cb.SelectionLength == 0)
            strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
        else
            strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
    }
    else
    {
        if (cb.SelectionLength == 0)
            strFindStr = cb.Text + e.KeyChar;
        else
            strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
    }
    int intIdx = -1;
    // Search the string in the ComboBox list.
    intIdx = cb.FindString(strFindStr);
    if (intIdx != -1)
    {
        cb.SelectedText = "";
        cb.SelectedIndex = intIdx;
        cb.SelectionStart = strFindStr.Length;
        cb.SelectionLength = cb.Text.Length;
        e.Handled = true;
    }
    else
        e.Handled = true;
}









谢谢。希望它有所帮助!





Thanks. Hope it helps!


这篇关于C#combobox只允许列表项但允许输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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