问题
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;
...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;
}
}
}
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.
Why, and how can I fix it?
回答1:
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.
来源:https://stackoverflow.com/questions/10323824/why-does-mousehover-event-only-get-called-if-a-messagebox-show-or-breakpoint-o