2009-08-12 16 views
9

あなたのために簡単なもの。他のコントロールにキーストロークを送信する

私はリストボックスの上にテキストボックスを持っています。

テキストボックスは、リストボックスのデータをフィルタリングするために使用します。

だからユーザーがテキストボックスに入力すると、下向き/上向き/ページ下/ページ上のキーストロークを「トラップ」してリストボックスにフォワードしたいと思います。

私はWin32 APIを使用してWM_KeyDownメッセージを送信することができます。しかし、これを行うには.NETの方法が必要です。

答えて

11

SendKeys.Send()メソッド。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      listBox1.Focus(); 
      SendKeys.Send(e.KeyChar.ToString()); 
     } 

ここでは、リスト項目を選択できるコードです。

private void Form1_Load(object sender, EventArgs e) 
     { 
      textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
      textBox1.AutoCompleteSource=AutoCompleteSource.CustomSource; 
      string[] ar = (string[])(listBox1.Items.Cast<string>()).ToArray<string>(); 
      textBox1.AutoCompleteCustomSource.AddRange(ar); 
     } 
     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
      listBox1.Text = textBox1.Text; 
     } 
+3

を語りますか – vIceBerg

+0

そして、もし私がフォーカスをテキストボックスにとどめたいなら、私はtextbox.focus()を直後にしますか? sendkeyにコントロールを渡すためのオーバーロードがありません... – vIceBerg

1

あなたは私たちがpreviewkeyupイベントを使用し、リストボックスをフィルタテキストボックスを持っている私たちのWPFアプリで

  listBox1.DataBindings.Add("DataSource", textBox1, "Text", true, DataSourceUpdateMode.OnPropertyChanged). 
      Format += (sender, e) => 
      { 
       e.Value = _strings.FindAll(s => s.StartsWith((string) e.Value)); 
      }; 
+0

別のコントロールにキーストロークを送信するためのデータバインディング?? – vIceBerg

+0

いいえ、リストボックスのデータをフィルタリングします。テキストボックスのTextプロパティをリストボックスのDataSourceプロパティにバインドし、DataSourceUpdateMode.OnPropertyChangedを使用すると、これを達成できます。 –

+0

チップありがとう – vIceBerg

0

をデータバインディングを使用することができます。コードの中で、どのキーが押されているかを調べることができます(私のコードは私の目の前にありません.e.Key == Key.UpArrowのようなものです。それがホットキーの1つである場合、それに応じてユーザーコントロールを操作します。

私たちはそれをユーザーコントロールに投げ込んで、NavigateableListboxなどと呼ばれるインターフェイスを実装し、MoveUp()、MoveDown()、PageUp()、PageDown()などを実装するように強制しました。イベントは、ちょうどどのように私はリストボックスコントロールにキーストロークを送信するためにSendKeys.Sendを教えていますか?もしe.Key = Key.UpArrow {mylistbox.MoveUp()}

1
private void textBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
    { 
     if (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) 
     { 
      // Setting e.IsInputKey to true will allow the KeyDown event to trigger. 
      // See "Remarks" at https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx 
      e.IsInputKey = true; 
     } 
    } 

    private void textBox_KeyDown(object sender, KeyEventArgs e) 
    { 
     string send = ""; 
     if (e.KeyCode == Keys.PageUp) 
     { 
      send = "PGUP"; 
     } 
     else if (e.KeyCode == Keys.PageDown) 
     { 
      send = "PGDN"; 
     } 
     else if (e.KeyCode == Keys.Up) 
     { 
      send = "UP"; 
     } 
     else if (e.KeyCode == Keys.Down) 
     { 
      send = "DOWN"; 
     } 
     if (send != "") 
     { 
      // We must focus the control we want to send keys to and use braces for special keys. 
      // For a list of all special keys, see https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx. 
      listBox.Focus(); 
      SendKeys.SendWait("{" + send + "}"); 
      textBox.Focus(); 
      // We must mark the key down event as being handled if we don't want the sent navigation keys to apply to this control also. 
      e.Handled = true; 
     } 
    } 
関連する問題