2017-12-02 12 views
0

TextBox의 AutoCompleteSourceAutoCompleteMode 속성을 사용하면 텍스트 상자에서 자동 완성 기능을 사용할 수 있습니다.C# check textbox autocomplete is empty

필자는 텍스트 상자의 AutoCompleteSource로 직접 datatable을 바인딩했으며 제대로 작동합니다.

소스에서 입력 단어를 사용할 수없는 상황에서는 자동 완료가 아무런 결과가 없으므로 이러한 상황에서 다른 작업을 수행해야합니다.

자동 완성 결과가 비어 있는지 어떻게 확인해야합니까?

답변

1

여기서는 취할 수있는 한 가지 방법이 있습니다. 다음 코드는 3 자 이상을 입력 할 때 텍스트 상자의 TextChanged 이벤트에서 제안 사항을 가져옵니다. 우리는 제안을 얻은 다음 제안이 반환되었는지 확인합니다. 예인 경우 AutoCompleteCustomSource을 설정합니다. 그렇지 않으면, 우리는 무엇을 하든지 할 것입니다.

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    TextBox t = sender as TextBox; 
    if (t != null) 
    { 
     // Here I am making the assumption we will get suggestions after 
     // 3 characters are entered 
     if (t.Text.Length >= 3) 
     { 
      // This will get the suggestions from some place like db, 
      // table etc. 
      string[] arr = GetSuggestions(t.Text); 

      if (arr.Length == 0) {// do whatever you want to} 
      else 
      { 
       var collection = new AutoCompleteStringCollection(); 
       collection.AddRange(arr); 

       this.textBox1.AutoCompleteCustomSource = collection; 
      } 
     } 
    } 
}