【发布时间】:2017-07-11 03:28:01
【问题描述】:
已经提出了类似的问题(例如,here),但是我还没有为我的具体案例找到答案。我正在构建一个基于 DevExpress 控件的自定义控件,该控件又基于标准 TextBox,并且我遇到了一个闪烁问题,这似乎是由于尝试更新选择的基本 TextBox 组件造成的。
在不解释我的自定义控件的所有细节的情况下,要重现该问题,您只需将 TextBox 放在 Form 内,然后使用以下代码:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.MouseMove += TextBox1_MouseMove;
}
private void TextBox1_MouseMove(object sender, MouseEventArgs e) {
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
}
}
如果您启动它,点击TextBox,然后向右移动光标,您会注意到闪烁问题(参见视频here)。对于我的自定义控件,我需要避免这种闪烁。我一定会使用TextBox(所以没有RichTextBox)。有什么想法吗?
【问题讨论】:
-
您是要完全避免选择,还是要允许选择但不应该在文本更改时闪烁?
-
我试过你的例子,点击后没有闪烁。应该是其他事件覆盖,可以分享其他事件吗
-
来自 MSDN
Be careful when you write code for a MouseMove handler. MouseMove will frequently occur while the user is interacting with the application or with the specific object area that has the handler. Any computationally or graphically intensive code in a MouseMove handler can potentially introduce a noticeable lag in how the mouse pointer (or stylus pointer) draws and how the application generally behaves.我会说它正在发生,因为它在鼠标移动期间会多次更新。 -
@IkramTurgunbaev 当您选择文本时会出现闪烁。左键单击文本框(并按住鼠标按钮)并在文本上移动以选择它。当鼠标移动处理程序更改文本时,蓝色选择颜色闪烁。
-
@RenéVogt 是的,你是对的,谢谢你的澄清
标签: c# winforms textbox custom-controls textselection