简体   繁体   English

在输入文本之前专注于 TextBox C#

[英]Focus on TextBox before text gets entered C#

I am trying to set the focus on a textbox using the below methods:我正在尝试使用以下方法将焦点设置在文本框上:

Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown += HandleKeyDown;
Windows.UI.Core.CoreWindow.GetForCurrentThread().CharacterReceived += HandleCharacter;

But the problem is the characters reach the screen before the textbox gets focused and so do not display properly.但问题是字符在文本框聚焦之前到达屏幕,因此无法正确显示。 Is there a way to delay the characters from appearing before the textbox is focused?有没有办法在文本框聚焦之前延迟字符出现?

Putting a Delay in HandleKeyDown sure delays the method but that does not stop CharacterReceived while the delay is still in progress.在 HandleKeyDown 中放置 Delay 肯定会延迟该方法,但这不会在延迟仍在进行中时停止 CharacterReceived。

Thanks谢谢

public void HandleKeyDown(Windows.UI.Core.CoreWindow window, Windows.UI.Core.KeyEventArgs e)

{

 if (e.VirtualKey== Windows.System.VirtualKey.F12)

{

//do something here for the characters that follow this key

Task.Delay(500);

//this task gets delayed but the other characters that follows f12 keypress keep showing up in the screen.

} }

} }

You can use the Shown event of main form.您可以使用主窗体的 Shown 事件。 In this method you put for example Textbox1.在此方法中,您输入例如 Textbox1。 Focus=True.焦点=真。 For example例如

Private void frmMainProgram_Shown(sender As System.Object, e As System.EventArgs) {
    TextBox1.Focus();
} 

Try with keyup property.尝试使用 keyup 属性。 It will give some delay and focus on keyup它会产生一些延迟并专注于 keyup

CoreWindow.KeyUp核心窗口.KeyUp

Was able to take care of this using能够处理这个使用

KeyboardDeliveryInterceptor kbinterceptor = KeyboardDeliveryInterceptor.GetForCurrentView();
kbinterceptor.IsInterceptionEnabledWhenInForeground = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM