简单的一个给你们。
我在一个列表框的上面有一个文本框,这个文本框是用来过滤列表框中的数据的。
这个文本框用来过滤列表框中的数据。
所以......当用户在文本框中输入时,我想 "捕获 "downuppagedownpageup的按键,并将其转发到列表框中。
我知道我可以使用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;
}
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;
}
}
你可以使用数据绑定
listBox1.DataBindings.Add("DataSource", textBox1, "Text", true, DataSourceUpdateMode.OnPropertyChanged).
Format += (sender, e) =>
{
e.Value = _strings.FindAll(s => s.StartsWith((string) e.Value));
};
在我们的wpf应用中,我们有一个过滤列表框的文本框,我们使用了预览keyup事件。 在代码里面,我们可以检查是什么键被按下了(前面没有我的代码,是类似于e.Key == Key.UpArrow,无论哪种方式,C#中都有一个内置的类来实现)。 如果是热键之一,我们就对用户控件进行相应的操作。
对于列表框,我们把它扔进一个用户控件中,并实现了一个接口,把它叫做NavigateableListbox或类似的东西,强迫它实现MoveUp()、MoveDown()、PageUp()、PageDown()等,所以文本框事件只是说if e.Key = Key.UpArrow { mylistbox.MoveUp() }。
SendKeys.Send()
该方法并不是在所有情况下都能使用,因为 Send()
方法可以向另一个Control寻址 !
在2020年,最好的解决方案是使用SendMessage()函数,以确保100%。
在VB.Net中,我使用了以下代码
Public Declare Auto Function SendMessage Lib "user32.dll"
( ByVal hWnd As IntPtr
, ByVal wMsg As Int32
, ByVal wParam As Int32
, ByVal s As Int32
) As Int32
Private Const WM_CHAR As Int32 = &H102
SendMessage(txtInput.Handle, WM_CHAR, AscW(sValue.Chars(0)), 0)
哪儿 txtInput
是一个样本 Textbox
控件。
在我的例子中,我点击html <div>
中定义的元素。WebView
控件,当我点击它或当鼠标移动到它上面时,它就会改变颜色。当我点击这个 <div>
调用VB.Net的一个ScriptNotify()方法,将一些字符输入到 Textbox
使用 SendMessage()
功能。
在旧的解决方案中,我认为,有时。WebView
先声夺人 SendKeys.Send()
方法,这样传播的密钥就不会被发送到 txtInput
Textbox.SendKeys.Send()函数可以发送你想要的东西。
你可以使用旧的解决方案,但可以肯定的是,总有一天,SendKeys.Send()函数会把你想要的东西发送到另一个Handle。