Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 只读Windows窗体组合框_.net_Winforms_Combobox - Fatal编程技术网

.net 只读Windows窗体组合框

.net 只读Windows窗体组合框,.net,winforms,combobox,.net,Winforms,Combobox,有没有办法使windows窗体组合框成为只读的? 具体来说:用户应该能够键入,但只允许框中的那些值(使用自动完成或从列表中选择) 还是使用验证事件的唯一方法 问候 Mario您可以将DropDownStyle设置为DropDownList,但这实际上不允许键入(但允许使用键盘进行选择) 如果您确实希望用户能够键入/查看不完整的单词,则必须使用事件。验证事件将是最佳选择 如果在用户键入内容时设置了AutoCompleteMode=SuggestAppend和AutoCompleteSource=L

有没有办法使windows窗体组合框成为只读的? 具体来说:用户应该能够键入,但只允许框中的那些值(使用自动完成或从列表中选择)

还是使用验证事件的唯一方法

问候


Mario

您可以将DropDownStyle设置为DropDownList,但这实际上不允许键入(但允许使用键盘进行选择)


如果您确实希望用户能够键入/查看不完整的单词,则必须使用事件。验证事件将是最佳选择

如果在用户键入内容时设置了
AutoCompleteMode=SuggestAppend
AutoCompleteSource=ListItems
,则组合框会自动显示以键入字符开头的条目

然后,通过处理
SelectedIndexChanged
SelectedValueChanged
事件,您将能够截获用户在值列表中键入的值

如果您也绝对不希望用户键入列表中没有的内容,那么是的,您必须处理例如
KeyDown
事件,如:

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
    char ch = (char)e.KeyValue;
    if (!char.IsControl(ch))
    {
        string newTxt = this.comboBox1.Text + ch;
        bool found = false;
        foreach (var item in this.comboBox1.Items)
        {
            string itemString = item.ToString();
            if (itemString.StartsWith(newTxt, StringComparison.CurrentCultureIgnoreCase))
            {
                found = true;
                break;
            }
        }
        if (!found)
            e.SuppressKeyPress = true;
    }
}

谢谢。除了KeyDown事件代码外,上述方法对我有效。因为组合框附加到数据表。如果combobox附加到DataTable,并且您绝对不希望用户键入列表中没有的内容,请尝试下面的代码

private void cmbCountry_KeyDown(object sender, KeyEventArgs e)
    {
        char ch = (char)e.KeyValue;
        if (!char.IsControl(ch))
        {
            string newTxt = this.cmbCountry.Text + ch;
            bool found = false;
            foreach (var item in cmbCountry.Items)
            {
                DataRowView row = item as DataRowView;
                if (row != null)
                {
                    string itemString = row.Row.ItemArray[0].ToString();
                    if (itemString.StartsWith(newTxt,     StringComparison.CurrentCultureIgnoreCase))
                    {
                        found = true;
                        break;
                    }
                }
                else
                    e.SuppressKeyPress = true;
            }
            if (!found)
                e.SuppressKeyPress = true;
        }
    }

谢谢,我怕我不得不这么做!谢谢我只是想不用自己编码就可以了。。。猜测我的集合的另一个库组件。。。