TextBox的动态填充自动完成功能无法在c#中实现吗?

时间:2016-09-13 07:53:35

标签: c# winforms textbox

我确实把这段代码放在了“TextChanged” - 事件中,但我认为那里“太晚了”:

Private void textTiteUltTest_TextChanged(object sender, EventArgs e) {

          TextBox tb = (TextBox)sender;
          DataLayer DBClass = new DataLayer();

          try {

            string SQL = ;
            string strVar = textTiteUltTest.Text + "%";

            //Get SqlDataReader from DataClass
            SqlDataReader sqlDR = DBClass.ReturnDataReader(SQL, , strVar);
            AutoCompleteStringCollection autoCol = new AutoCompleteStringCollection();

            //fill stringcollection
            while (sqlDR.Read()) {
              autoCol.Add(sqlDR["TITE_TITLES"].ToString());
            }

            //fill autocomplete textbox
            lock (tb.AutoCompleteCustomSource.SyncRoot) {
              tb.AutoCompleteCustomSource = autoCol;
            }
          }

          catch (Exception exc) {
            Console.WriteLine(exc.Data.ToString() + exc.Message.ToString());
          }

          finally {
            //cleanup
            DBClass.Dispose();
            Console.WriteLine("'textTiteUltTest_TextChanged'");
          }

        }

        error -> AccessViolationException:
        "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
        Data: {System.Collections.ListDictionaryInternal}


        not in the try/catch-block, but here: 

            static class Program
            {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
            }

所以我认为,当我在文本框中输入时,TextChangded-event中的代码在一个线程中填充autocomplete-stringcollection,内部textbox-method / function需要同时从autocomplete-stringcollection中读取在一个不同的主题。

SyncRoot的锁定根本没有帮助,但在这种情况下,我甚至不确定我是否正确。

此外,我不得不说这段代码有时有效,有时会崩溃。我认为这取决于获取sql server结果集的速度。

这可能吗?

我也经历过这个link1link2,但他们还没回答,所以请帮忙解决这个问题

我得到的确切错误是:System.AccessViolationException 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

错误的堆栈跟踪     System.AccessViolationException:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。        at ICSharpCode.TextEditor.TextArea.HandleKeyPress(Char ch)        at ICSharpCode.TextEditor.TextArea.SimulateKeyPress(Char ch)        在ICSharpCode.TextEditor.TextArea.OnKeyPress(KeyPressEventArgs e)        在System.Windows.Forms.Control.ProcessKeyEventArgs(消息&amp; m)        在System.Windows.Forms.Control.ProcessKeyMessage(消息&amp; m)        在System.Windows.Forms.Control.WmKeyChar(消息&amp; m)        在System.Windows.Forms.Control.WndProc(消息&amp; m)        在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)        在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)        在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
   at System.Windows.Forms.RichTextBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

1 个答案:

答案 0 :(得分:0)

在表单加载期间绑定文本框而不是textchanged event.TextBox将自动显示用户输入的filterd建议。

    List<string> s = new List<string>();
    s.Add("abc");
    s.Add("aaa");
    s.Add("acb");           

    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection data = new  AutoCompleteStringCollection();
    data.AddRange(s.ToArray());

    textBox1.AutoCompleteCustomSource = data;