0

我有一个简单的表单,里面有一个文本框,我正在尝试制作一个用户友好的自动建议文本框..

根据当前场景,我正在使用AutoCompleteStringCollection类,通过该类,我可以使文本框显示以在文本框中输入的特定文本开头的单词的建议。但是,我想让我的程序即使在某个部分也应该显示建议来自数据库的字符串与 Textbox.Text 匹配。

目前,我可以使用 . 根据 userInput 过滤来自 DB 的数据dataView。但我仍然无法在前端显示输出。

我已经尝试了所有文本框事件,例如 ' KeyPress', KeyDown, KeyUpTextChanged但它不起作用....

我的代码::

         public partial class Form2 : Form
        {
            AutoCompleteStringCollection autoCompletefromDB = new AutoCompleteStringCollection();
            AutoCompleteStringCollection searchResults = new AutoCompleteStringCollection();


            MyLinqDataContext dtcontext = new MyLinqDataContext();
          //  static string searchChar = "";
            SqlConnection con = new SqlConnection("Data Source=DATASERVER\\SQL2K8;Initial Catalog=VTMMedicalContent;Persist Security Info=True;User ID=vtm;Password=M3d!c@l");
            DataTable dTable = new DataTable();
            SqlCommand cmd;
            SqlDataAdapter da;
            DataView dtView;
            public Form2()
            {
                InitializeComponent();
            }

            private void Form2_Load(object sender, EventArgs e)
            {
                cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select DiagnosisName from [VTMMedicalContent].[dbo].[DiagnosisMaster]";
                da = new SqlDataAdapter(cmd);
                da.Fill(dTable);
                dtView = new DataView(dTable);

            }

     //And My KeyPress Event Code..

 private void txtAutoComplete_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar))
            {
                dtView.RowFilter = dtView.Table.Columns[0].ColumnName + " Like '%" + e.KeyChar + "%'";
                foreach (DataRowView dtViewRow in dtView)
                    searchResults.Add(dtViewRow[0].ToString());

                txtAutoComplete.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txtAutoComplete.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtAutoComplete.AutoCompleteCustomSource = searchResults;
            }
            //MessageBox.Show("The Elements in searchResult are:" + searchResults.Count);
        }

我尝试在KeyDown, KeyUp,TextChanged事件中编写相同的代码,但没有用..:(

它仅适用于 Form_Load 但仅显示与单词起点匹配的建议..

4

1 回答 1

0

创建一个自动完成文本框控件

自动完成功能有几个怪癖,这些怪癖是从其最初的设计用途继承而来的,即 Internet Explorer 的地址框。这包括在您单击列表中的项目时发出 Enter 键。在 IE 的地址框中按 Enter 可以导航到输入的 URL。

您对此无能为力,本机界面 (IAutoComplete2) 几乎没有选项来配置其工作方式。它通过伪造 Windows 消息将击键插入文本框。这是您可以区分的一种方法,实际的密钥不会被关闭。您可以通过调用 GetKeyState() 来检查某些内容,例如:作为示例,您可以随意处理这个问题

    private void textBox1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == Keys.Enter && GetKeyState(Keys.Enter) < 0) {
            Console.WriteLine("Really down");
        }
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern short GetKeyState(Keys key);
于 2012-08-21T14:02:00.547 回答