Shows MessageBox on MouseDown, sucks MouseUp event

I am working on a WPF application in which I handled the mouse down event, which eventually pops up a MessageBox. But after the MessageBox appears in mouseDown, it eats up the corresponding MouseUp event of the control.

The script can be easily reproduced by simply handling the MouseDown and MouseUP event in the WPF window as: -

private void Window_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.show("Hello, Mouse down"); } private void Window_MouseUP(object sender, MouseButtonEventArgs e) { MessageBox.show("Hello, Mouse Up"); } 

The MouseUp message is never displayed after the message appears in the MouseDown event.

+7
source share
3 answers

How about initializing a new instance of System.Threading.Thread to call MessageBox so that the main UI thread is not interrupted by a prompt?

Example

 private void Window_MouseDown(object sender, MouseEventArgs e) { Thread mythread = new Thread(() => MessageBox.Show("Hello, Mouse Down")); //Initialize a new Thread to show our MessageBox within mythread.Start(); //Start the thread } private void Window_MouseUP(object sender, MouseEventArgs e) { Thread mythread = new Thread(() => MessageBox.Show("Hello, Mouse Up")); //Initialize a new Thread to show our MessageBox within mythread.Start(); //Start the thread } 

Screenshot

Calling a MessageBox in a different thread to avoid interrupting the main user interface thread

Thanks,
Hope you find this helpful :)

+2
source

As the commentator noted in your original post, it seems that what happens here is that the user mouse wanders out of focus to click in the message box or even just display it, so the mouse is still “up” - the event is never triggered. If you just want to display message boxes, just use:

 private void Window_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.show("Hello, Mouse down"); MessageBox.show("Hello, your mouse must be up because you've shifted focus!"); } 

must do the job. If this behavior is repeated for something like changing the window title or something that does not require user input, then this may be a problem, but I am 100% sure that this is only a problem with the MessageBox. Hope this helps.

0
source

@picrofo solution is also good and easy, but I did it by

  DialogResult result; private void button1_MouseDown(object sender, MouseEventArgs e) { string message = "would you like to see mouse up event?"; string caption = "event trick"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; result = MessageBox.Show(message, caption, buttons); textBox1.Text = result.ToString(); if (result == System.Windows.Forms.DialogResult.Yes) { button1_MouseUp(sender, e); } } 
0
source

All Articles