他のコントロールにキーストロークを送信
-
13-09-2019 - |
質問
君たちのために簡単なもの。
私は、リストボックスの上にテキストボックスを持っています。
テキストボックスは、リストボックスでTHERデータをフィルタリングするために使用されます。
だから... ...場合はテキストボックスにユーザタイプ、私は「罠」ダウン/アップ/ PAGEDOWN / PAGEUPキー入力やリストボックスにそれらをフォワーディング関連したいと思います。
私が知っている私は、Win32 APIを使用し、WM_KEYDOWNメッセージを送信することができます。しかし、これを行うには、いくつかの.NET方法がなければならないです。
解決
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;
}
他のヒント
は、
データバインディングを使用することができます listBox1.DataBindings.Add("DataSource", textBox1, "Text", true, DataSourceUpdateMode.OnPropertyChanged).
Format += (sender, e) =>
{
e.Value = _strings.FindAll(s => s.StartsWith((string) e.Value));
};
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;
}
}
私たちのWPFアプリでは、我々はpreviewkeyupイベントを使用し、リストボックスをフィルタテキストボックスを持っています。コードの中に、私たちはキーが押されたかどうか確認することができます(私の前に私のコードを持っていないが、それはe.Key == Key.UpArrow、このためのC#でクラスに建てられたがありますいずれかの方法のようなものです)。それはホットキーの一つだ場合、我々はそれに応じてユーザーコントロールを操作します。
は、リストボックスのために我々はユーザーコントロールにそれを投げたとのインタフェースを実装し、それを呼ばNavigateableListboxまたはそのような何か、テキストボックスのイベントようMOVEUP()、MoveDown()、PageUpキー()、PageDownキー()などを実装することを余儀なくさちょうど言うe.Key = Key.UpArrowなら{mylistbox.MoveUp()}