按键呼叫功能

时间:2017-09-12 20:04:01

标签: c# function combobox

我正在使用以下代码在组合框中自动填充条目数据

private void AutoCompleteCombo(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;
}

它工作正常....但我试图在我的组合框事件按键中调用它

private void cmbState_KeyPress(object sender, KeyPressEventArgs e)
{
    AutoCompleteCombo();
}

但它给了我一个错误

  

没有任何论据符合所要求的形式   参数'sender'的'FRM_AddClient.AutoCompleteCombo(object,   KeyPressEventArgs)'

抱歉......我是编程新手

谢谢

2 个答案:

答案 0 :(得分:1)

尝试将处理程序中的参数传递给AutoCompleteCombo方法:

private void cmbState_KeyPress(object sender, KeyPressEventArgs e)
{
    AutoCompleteCombo(sender, e);
}

答案 1 :(得分:1)

正如您所看到的,您的方法会收到两个类型为&#34; object&#34;和&#34; KeyPressEventArgs&#34;。
private void AutoCompleteCombo(object sender, KeyPressEventArgs e)
因此,当您尝试执行方法时,必须以与预期相同的方式传递此类对象的参数。

试试这个:

AutoCompleteCombo(sender, e);


祝你有个美好的一天!