返回首页

{A}
{S0}简介
这是一个简单的代码片段,它是用来使一个自动完成的ComboBox的。使用代码
用途:调用函数在ComboBox的KeyPress事件处理程序中的自动完成。

AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)



// AutoComplete

public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)

{

    this.AutoComplete(cb, e, false);

}



public void AutoComplete(ComboBox cb, 

       System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)

{

    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 = blnLimitToList;

    }

}
历史2006年11月8日,日发布。

回答

评论会员:。fullfill我的要求 时间:2011/12/07
Tabitutza
评论会员:好 时间:2011/12/07
imresoft
评论会员:游客 时间:2011/12/07
!上帝保佑你Tabiiiiiiiii
麦吉
评论会员:游客 时间:2011/12/07
伟大的工作,但我有一点点问题如果你写一个组合框的文本例如AA完成亚伦亚历如果chkAutoComplete被选中。如果你想要写Aaren的,然后在ComboBox不启用此(我把项目除外)。我们可以解决这个如果我们删除行"e.Handled=blnLimitToList;"在源码。之后自动完成功能工作良好,但我们可以选择写在ComboBox中的任何
。chris175
评论会员:游客 时间:2011/12/07
您解决了我的一个很大的问题。imgsrc=http://www.orcode.com/upimg/2011_12_07_04_45_39_1.gifimgsrc=http://www.orcode.com/upimg/2011_12_07_04_45_39_2.gif
chris175
评论会员:游客 时间:2011/12/07
也许代替(CHAR)8,你应该使用(CHAR)Keys.Back克里斯
Almustafa
评论会员:游客 时间:2011/12/07
"AA"的类型,然后按"\"键。现在存在无效的文本。克里斯
sameeraperera
评论会员:游客 时间:2011/12/07
我认为是:(this.chkAutoComplete.Checked)this.AutoComplete(this.cbAutoComplete,E,this.chkLimitToList.Checked);看你imgsrc=http://www.orcode.com/upimg/2011_12_07_04_45_39_3.gif