2016-11-18 7 views
0

コンボボックスに項目が入力されたときにチェックする方法はありますか?さらに説明すると、リストの外にあるものが選択されていれば、その入力を受け入れません。私はstackoverflow内を見てきましたが、唯一の解決策は、ドロップダウンリストスタイルに私のcomboBoxスタイルを変更することです。この問題は、100個以上のレコードが選択されているため、comboBoxのオートコンプリートは、入力されたユーザー入力によってこれらをフィルタリングするために絶対に必要です。更新リスト内の項目をチェックするオートコンプリートコンボボックスが入力されました

(グローバルマッチした宣言):

private void comboBox3_TextChanged(object sender, EventArgs e) 
    { 
     ComboBox c = ((ComboBox)sender); 
     string[] items = c.Items.OfType<string>().ToArray(); 
     matched = items.Any(i => i == c.Text.Trim().ToLower()); 

    } 

し、実行する場所です:

private void button5_Click(object sender, EventArgs e) 
    { 

     if (matched==false) 
     { 
      MessageBox.Show("Value in Carimed Items does not exist"); 
     }else 
     { 


      if (string.IsNullOrEmpty(comboBox5.Text)) 
     { 
      MessageBox.Show("Please select output file to be written to!"); 
     } 
     else 
     { 

      // int current = 0; 
      if (comboBox1.Text.Trim() == string.Empty) 
      { 
       MessageBox.Show("All fields must be filled in before saving!"); 

      } 
      else 
      { 


        // StringBuilder csvconten = new StringBuilder(); 
        // csvconten.AppendFormat("{0},{1},{2},{3},{4},{5}\r\n", comboBox2.Text, textBox5.Text, textBox2.Text, comboBox3.Text, textBox3.Text, comboBox1.Text); 
        // string csvpath = "cross_check.csv"; 
        // File.AppendAllText(csvpath, csvconten.ToString()); 

        string connectionString3 = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacy_Output_File;Integrated Security=True"; 
        string query3 = "INSERT INTO dbo.[" + comboBox5.Text + "] VALUES('" + comboBox2.Text + "','" + textBox5.Text.Replace("'", "''") + "','" + textBox7.Text.Replace("'", "''") + "','" + textBox2.Text.Replace("'", "''") + "','" + comboBox3.Text.Replace("'", "''") + "','" + textBox3.Text + "','" + comboBox1.Text + "');"; 

        using (SqlConnection connection = new SqlConnection(connectionString3)) 
        { 
         SqlCommand command = new SqlCommand(query3, connection); 

         command.Connection.Open(); 
         command.ExecuteNonQuery(); 
         command.Connection.Close(); 

        } 


        // textBox1.Clear(); 
        //  textBox3.Clear(); 
        // comboBox3.ResetText(); 

        textBox2.Clear(); 
        textBox3.Clear(); 
        comboBox3.ResetText(); 
        comboBox1.ResetText(); 
       } 

       string connectionString2 = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True"; 
       string query2 = "UPDATE Liguanea_Lane2 SET Progress= '1' where code = '" + comboBox2.Text + "'; "; 


       using (SqlConnection connection = new SqlConnection(connectionString2)) 
       { 
        SqlCommand command = new SqlCommand(query2, connection); 
        command.Connection.Open(); 
        command.ExecuteNonQuery(); 
       } 
       //this.liguanea_ProgressTableAdapter1.Fill(this.pharmaciesDataSet7.Liguanea_Progress); 
       comboBox2.SelectedIndex = comboBox2.SelectedIndex + 1; 
       //current = liguaneaLane2BindingSource.Position; 
       //this.liguanea_Lane2TableAdapter.Fill(this.pharmaciesDataSet3.Liguanea_Lane2); 
       refreshDataGrid2(); 
       if (dataGridView1.CurrentRow != null) 
       { 

        dataGridView1.CurrentCell = 
         dataGridView1 
         .Rows[Math.Min(dataGridView1.CurrentRow.Index + 1, dataGridView1.Rows.Count - 1)] 
         .Cells[dataGridView1.CurrentCell.ColumnIndex]; 
        // liguaneaLane2BindingSource.Position = Math.Min(current + 1, liguaneaLane2BindingSource.Count - 1); 
       } 
      } 
     } 
    } 
+0

:グローバルTextChangedイベントは、その値を代入することを形で、あなたのような他の方法でそれを使用することができますか? – DeadlyJesus

+0

それを試しましたが、それでもユーザーからの自由なタイピングが可能です。彼らは何でも入力できるという意味です。 – Jevon

+0

'ComboboxStyle = ComboBoxStyle.DropDownList'でも? – DeadlyJesus

答えて

0

あなたが入力したテキストがあなたにexsistsかどうかを確認するために、コンボボックスのTextChangedイベントを使用することができますリスト:

private void comboBox1_TextChanged(object sender, EventArgs e) 
     { 
      ComboBox c = ((ComboBox)sender); 
      string[] items = c.Items.OfType<string>().ToArray(); 
      bool matched = items.Any(i => i == c.Text.Trim().ToLower()); 
     } 

matchedブールシンプルな `AutoCompleteMode = SuggestAppend`トリックを行うことはありません

void Button_Click(object sender, e EventArgs){ 
     if(matched) 
    { 
     //do something 
    } else{ 
     // show an error message 
    } 

} 
+0

私は例を挙げてもよろしいですか? – Jevon

+0

私は –

+0

の例を追加しましたが、一致するものが見つかったとしても値が存在しないようにポップアップし続けます – Jevon

関連する問題