当窗体没有焦点时,RichTextBox不会在鼠标按下时开始选择 [英] RichTextBox doesn't start selection on mouse down when the form has not focus
问题描述
我正在使用WinForms,在我的窗体上有一个RichTextBox.当我的表单失去焦点但可见时,并且我尝试突出显示/选择文本时,直到表单或文本框本身具有焦点时,它才允许我这么做.
I'm using WinForms and on my Form I have a RichTextBox. When my form is out of focus but visible and I try to highlight/select text, it does not allow me to until the form or textbox itself has focus.
我尝试过:
txtInput.MouseDown += (s, e) => { txtInput.Focus(); }
但无济于事,而且我似乎无法在网上找到有关此问题的任何信息.
but to no avail and I can't seem to find anything online about this issue.
在使用其他程序(例如记事本)进行测试时,它确实具有所需的行为.
When testing with another program like Notepad, it does possess the desired behavior.
推荐答案
MouseDown
为时已晚.
这肯定是一种解决方法,但可能仅是您需要的:
This is a workaround for sure, but may be all you need:
private void txtInput_MouseMove(object sender, MouseEventArgs e)
{
txtInput.Focus();
}
或者当然是
txtInput.MouseMove += (s, e) => { txtInput.Focus(); }
按原样,当您移至文本框上方时,它可能会从表单上的其他控件中夺取焦点.如果这是个问题,您可以使用
As it is it may steal focus from other controls on your form when you move over the textbox. If this is a problem you could prevent it by checking if your program is active using one the of answers here..
这篇关于当窗体没有焦点时,RichTextBox不会在鼠标按下时开始选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!