2

在 c# 中,我有来自 listView 的带有自动完成源的文本框。当我键入密钥时,文本框会填充建议。当我从列表中选择任何一个建议的文本时,它的工作正常。但是,当我尝试使用键盘(向下箭头)在填充列表中向下移动时,文本框的文本将替换为列表中的第一项。我无法移至列表中的第二项。

private void textbox1_TextChanged(object sender, EventArgs e)
{
    if (textbox1.Text != "")
    {
        TreeNode treeNode = TreeNode;
        string text = textbox1.Text;
        Boolean isContain = false;

        foreach (TreeNode subNode in treeNode.Nodes)
        {
            if (subNode.Name.StartsWith(text))
            {
                if (subNode.Name.StartsWith(text))
                    suggestStageName.Add(subNode.Name);
                isContain = true;
                if (subNode.Name.Equals(textbox1.Text))
                   do corresponding event
            }
        }
        if (isContain)
        {
            textbox1.AutoCompleteCustomSource = suggestStageName;
            textbox1.AutoCompleteMode = AutoCompleteMode.Suggest;                 
            textbox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }
    }
}       
4

1 回答 1

3

此段不应位于 textChanged 事件内。您应该只分配一次。

textbox1.AutoCompleteCustomSource = suggestStageName;
textbox1.AutoCompleteMode = AutoCompleteMode.Suggest;                 
textbox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

编辑:

尝试这个:

private void Form1_Load(object sender, EventArgs e)
{

    AutoCompleteStringCollection suggestStageName = new AutoCompleteStringCollection();

    foreach (TreeNode subNode in treeNode.Nodes)
    {
        suggestStageName.Add(subNode.Name);
    }

    textBox1.AutoCompleteCustomSource = suggestStageName;
    textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

}

并将 textChanged 事件留空。自动完成将为您完成所有工作。

于 2012-08-24T09:06:41.237 回答