ユーザーフレンドリーな自動提案テキストボックスを作成しようとしているテキストボックスを含む単純なフォームがあります..
現在のシナリオではAutoCompleteStringCollection
、テキストボックスに入力された特定のテキストで始まる単語の提案をテキストボックスに表示させることができるクラスを使用しています。データベースからの文字列の一部が Textbox.Text と一致します。
現在、userInput を使用して、DB からのデータをフィルタリングできます。しかしdataView
、フロント エンドで出力を表示することはまだできません。
KeyPress
' ', KeyDown
,KeyUp
のようなすべてのテキスト ボックス イベントを試しTextChanged
ましたが、うまくいきません....
マイコード::
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 でのみ機能しますが、単語の開始点に一致する提案のみが表示されます..