为什么只有在出现messagebox.show()或断点之前才调用MouseHover事件? [英] Why does MouseHover event only get called if a messagebox.show() or breakpoint occurs before it?
问题描述
我有一个布尔值,用于跟踪FlowLayoutPanel上的鼠标右键事件和相应的后续MouseUpEvent之间的状态:
I've got a boolean that keeps track of the state between a right-button MouseDown event on a FlowLayoutPanel and the corresponding subsequent MouseUpEvent:
bool TextBoxesRespondingToMouseMoveEvents = false;
...这是FlowLayoutPanel的MouseDown和MouseUp事件中的代码,以及FlowLayoutPanel上所有TextBox共享的共享MouseHover处理程序:
...Here's the code in the FlowLayoutPanel's MouseDown and MouseUp events, and a shared MouseHover handler that all TextBoxes on the FlowLayoutPanel share:
private void flowLayoutPanelGreatGooglyMooglyMain_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
TextBoxesRespondingToMouseMoveEvents = true;
//MessageBox.Show("TextBoxesRespondingToMouseMoveEvents is now true");
selectionStart = PointToClient(MousePosition);
}
}
private void flowLayoutPanelGreatGooglyMooglyMain_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
TextBoxesRespondingToMouseMoveEvents = false;
selectionEnd = PointToClient(MousePosition);
}
}
// This event is shared by all of the buttons on flowLayoutPanelGreatGooglyMooglyMain
private void textBoxQH1_MouseHover(object sender, EventArgs e) {
if (TextBoxesRespondingToMouseMoveEvents) {
TextBox tb = (TextBox)sender;
if (tb.BackColor.Equals(SystemColors.Window)) {
tb.BackColor = System.Drawing.Color.Gainsboro;
}
}
}
只要没有注释掉MouseDown事件中的MessageBox.Show(),或者如果我在该事件中有断点,此工作就起作用.但是,如果只是让代码运行,并且在MessageBox.Show()被注释掉的情况下,MouseHover()事件将永远不会触发.
This WORKS as long as the MessageBox.Show() in the MouseDown event is not commented out, OR if I have a breakpoint in that event. If you just let the code run, though, with the MessageBox.Show() commented out, the MouseHover() event never fires.
为什么以及如何解决?
推荐答案
看看这个问题;尽管它们没有直接说明没有/没有消息框(或断点)之间的行为差异,但它们开始说明文本框将吞噬鼠标事件,而您应该钩住PreviewXXX事件.
Have a look at the answers for this question; while they don't directly explain the difference in behavior between not/having the MessageBox (or breakpoint), it begins to explain that the textbox will be swallowing mouse events, and that you should hook the PreviewXXX events instead.
这篇关于为什么只有在出现messagebox.show()或断点之前才调用MouseHover事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!