2016-09-13 11 views
1

私は「TextChanged」-eventにこのコードを入れましたが、私はそれが「遅すぎる」と思う:TextBoxの動的塗りつぶしオートコンプリートが機能しないのはC#で可能ですか?

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・イベントからのコードを入力していながら、私は、と思います1つのスレッドでautocomplete-stringcollectionを埋めていて、内部のtextbox-method /関数は、別のスレッドでautocomplete-stringcollectionから同時に読み取る必要があります。

SyncRootのロックはまったく役に立ちませんが、私がこのケースで正しく作ったかどうかはわかりません。

また、このコードが動作し、時にはクラッシュすることがあります。私はそれがSQL Serverの結果を取得する速度に依存すると思う。

これは可能ですか?

ので、この

を解決するために助けてください、私は取得しています正確なエラーがこれである私も、このlink1link2を通過していますが、彼らはまだ答えていない:System.AccessViolationException は、保護されたメモリを読み書きしようとしました。これはしばしば、他のメモリが壊れていることを示します。

エラー System.AccessViolationExceptionのスタックトレースは:保護されたメモリを読み書きしようとしました。これはしばしば、他のメモリが壊れていることを示します。 System.Windows.Forms.Control.ProcessKeyEventArgsでICSharpCode.TextEditor.TextArea.OnKeyPress(KeyPressEventArgs e)にICSharpCode.TextEditor.TextArea.SimulateKeyPress(CHARのCH) でICSharpCode.TextEditor.TextArea.HandleKeyPress(CHARのCH) で System.Windows.Forms.Control.WndProcでSystem.Windows.Forms.Control.WmKeyChar(メッセージ& M) (メッセージでSystem.Windows.Forms.Control.ProcessKeyMessage(メッセージ& M)に(メッセージ& M) & m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(メッセージ& m) at System.Windows.Forms.Control.ControlNativeWindo代わりtextchanged event.TextBoxの形態負荷時w.WndProc System.Windows.Forms.NativeWindow.Callbackで(メッセージ& M) (のIntPtr hWndは、MSGのInt32、のIntPtr WPARAM、LPARAMのIntPtr)

+0

あなたの投稿に例外/エラーメッセージを投稿すると分かりやすいでしょう – cscmh99

答えて

0
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) 

バインドテキストボックスユーザーが入力したフィルタ提案が自動的に表示されます。

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; 
関連する問題