按钮从文本框中删除焦点c# [英] Button takes away focus from the text box c#
本文介绍了按钮从文本框中删除焦点c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我用c#中的按钮制作了一个键盘(触摸屏)我想写按钮点击功能作为退格键但每次按下backspace我专注的texbox失去了它的焦点创建错误只需键盘就好了
Hello,
I have made a keyboard(touchscreen) out of buttons in c# I want to write the function for button click as backspace but every time I press backspace my focused texbox loosing its focus creating errors working with just keyboard it fine
int index = focusedTextbox.SelectionStart;
focusedTextbox.Text = focusedTextbox.Text.Remove(focusedTextbox.SelectionStart - 1, 1);
focusedTextbox.Select(index - 1, 1)
谢谢你的帮助
thank you for your help
推荐答案
当然它会带走焦点。问题有点棘手。我不知道你正在使用什么UI框架/库,但我为System.Windows.Forms
提供了一个全面的解决方案:
应用程序焦点的输入和丢失 [ ^ ]。
如果你需要别的东西,请解释一下。
另一种方法,更通用的方法就是这样:因为虚拟键盘的目的是为了如果没有物理键盘,通过鼠标或触摸屏,您根本不需要键盘焦点和导航。因此,您可以构建纯粹不可聚焦元素的键盘,而不是按钮。它可以是Panel
,或者,对于WPF,TextBlock
,Border
与否。 甚至按钮
可以变得不可聚焦;请阅读按钮类型的MSDN文档。 [结束编辑]
现在,如果您通过任何更改需要创建系统范围的虚拟键盘,则需要使用SendInput
。为了以防万一,请查看我过去对该主题的回答:
如何在WPF按钮点击按键按键事件 [ 3 ],
如何在WPF按钮上点击按键的按键事件 [ 1 ],>
创建虚拟键盘 [ ^ ],
重点关注应用程序失败 [ ^ ],
在BACKSPACE按钮上编程 [ ^ ] 。
-SA
Of course it will take away focus. The problem is somewhat tricky. I don't know what UI framework/library you are using, but I provided one comprehensive resolution forSystem.Windows.Forms
:
Application focus getting and losing[^].
If you need something else, please explain.
Another approach, more universal one, would be this: as the purpose of virtual keyboard is to be used without a physical keyboard, through a mouse or touch screen, you don't need keyboard focus and navigation at all. So, you can compose your keyboard of purely non-focusable elements, not buttons. It can bePanel
, or, with WPF,TextBlock
, withBorder
or not. And evenButton
can be made non-focusable; please read the MSDN documentation on your button type. [END EDIT]
Now, if you, by any change, need to create a system-wide virtual keyboard, you would need to useSendInput
. Just in case, please see my past answers on the topic:
How to fire a Key Press event of a Key on button click in WPF [3],
How to fire a Key Press event of a Key on button click in WPF [1],
Creating a Virtual Keyboard[^],
Application focus getting and losing[^],
Programming on BACKSPACE button[^].
—SA
ButtonClickEventHandler(....)
{
string text = focusedtextbox.Text;
string newText = text.Substring(0, text.Length - 1);
Dispatcher.CurrentDispatcher.BeginInvoke(() => focusedtextbox.text = newText);
Dispatcher.CurrentDispatcher.BeginInvoke(() => focusedtextbox.Focus());
}
这篇关于按钮从文本框中删除焦点c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文