Showing MessageBox on MouseDown eats up MouseUp event - c#

I am working on a WPF application where i handled a mouse down event which eventually shows up
MessageBox.. But after MessageBox appears on mouseDown, it eats up corresponding MouseUp event of a control.
Scenario can be easily reproduced by simply handling MouseDown and MouseUP event in 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");
}
MouseUp message is never shown, once messagebox appears on MouseDown event.

What about initializing a new instance of System.Threading.Thread to call the MessageBox so that the main user interface thread would not be interrupted by the 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
Thanks,
I hope you find this helpful :)

As the commenter on your original post said, it seems that what's happening here is that the user's mouse wanders out of focus to click in the message box, or even just to display it, so the mouse moves "up" anyway - the event is never called.
If you're just wanting to display messageboxes, then simply using:
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!");
}
should do the job. If this behaviour repeats for something like changing a window title, or anything that doesn't require user input, then this could be a problem, but I am 100% sure that this is just an issue with regards to the MessageBox. Hope this helped.

#picrofo solution is also good and easy but i did by this way
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);
}
}

Related

C# mousedown event continously press key

okay so what I want to do, it when the mouse is HELD DOWN I want it to continously press a key. it should continously press this key until I let off.
Imagine if you will, a left and right button on a windows form,
then by clicking and holding the right button, the letter "R" displays on a textbox continously until you release the button. What I am doing has very little to do with that scenario, but you get what I'm trying to do here.
What exactly do I put in the mouse down to keep the sendkeys going forever without locking up the application?
I hope my question makes sense. lol.
Thanks
RT
private void pictureBoxKeyboard_MouseDown(object sender, MouseEventArgs e)
{
//something goes here
}
This is worth a read...
http://msdn.microsoft.com/en-us/library/ms171548.aspx
SendKeys.Send("r")
This might just fire once, you may want to attach a timer that starts on the MouseDown event and stops on the MouseUp event. Then you could put the SendKeys in the Timer.Tick event.
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 500;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
this.Text = "moose-Up";
}
private void button1_MouseDown(object sender, EventArgs e)
{
timer1.Start();
this.Text = "moose-Down";
this.richTextBox1.Select();
}
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send("r");
Debug.Print("tickling");
}
Select the control that you wish to receive the SendKeys value...

Mouse cursor not appearing as desired when using background process

I am running a long process as a background process as the process makes my UI unresponsive.
Now the issue is that, while the process is running as a background process, I want to display a wait cursor. I tried using dispatcher to update the cursor, but it does not work.
My process runs on button click:
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
backgroundworkerprocess.RunWorkerAsync();
}
In the backgroundworkerprocess event:
void backgroundworkerprocess_DoWork(object sender, DoWorkEventArgs e)
{
this.dispatcher.invoke(DispatcherPriority.Background,
new action((delegate) {
this.cursor = cursors.wait
})
);
}
I was hoping this would cause a wait cursor to be displayed anywhere on the form, but it's only showing up when the mouse is over the button.
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
backgroundworkerprocess.RunWorkerAsync();
}
void backgroundworkerprocess_DoWork(object sender, DoWorkEventArgs e)
{
// do work
}
Then just simply change the Cursor back in the RunWorkerCompleted event.
I dont think people understand you.
try this, sending it to Dispatcher:
Window.GetWindow(this).Cursor = Cursors.WaitCursor;

Mouse events not fired

I'm making a C# WinForms application. The MouseMove and MouseClick events of the form aren't getting fired for some reason. (I'm probably going to feel like an idiot when I find out why.)
It is a transparent form (TransparencyKey is set to the background colour) with a semi-transparent animated gif in a Picture Box. I am making a screensaver.
Any suggestions?
EDIT:
MainScreensaver.cs
Random randGen = new Random();
public MainScreensaver(Rectangle bounds)
{
InitializeComponent();
this.Bounds = Bounds;
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle screen = Screen.PrimaryScreen.Bounds;
Point position = new Point(randGen.Next(0,screen.Width-this.Width)+screen.Left,randGen.Next(0,screen.Height-this.Height)+screen.Top);
this.Location = position;
}
private void MainScreensaver_Load(object sender, EventArgs e)
{
Cursor.Hide();
TopMost = true;
}
private Point mouseLocation;
private void MainScreensaver_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseLocation.IsEmpty)
{
// Terminate if mouse is moved a significant distance
if (Math.Abs(mouseLocation.X - e.X) > 5 ||
Math.Abs(mouseLocation.Y - e.Y) > 5)
Application.Exit();
}
// Update current mouse location
mouseLocation = e.Location;
}
private void MainScreensaver_KeyPress(object sender, KeyPressEventArgs e)
{
Application.Exit();
}
private void MainScreensaver_Deactive(object sender, EventArgs e)
{
Application.Exit();
}
private void MainScreensaver_MouseClick(object sender, MouseEventArgs e)
{
Application.Exit();
}
Excerpt from MainScreensaver.Designer.cs InitialiseComponent()
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseClick);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseMove);
This isn't an answer to you question, but I'm leaving this answer in case anyone else stumbles upon this question while trying to debug this same issue (which is how I got here)
In my case, I had a class that was derived from Form.
I was also using TransparencyKey.
Some things I noticed
The events will not fire on the transparent parts of the form.
The events will not fire if the mouse cursor is over another control on the form.
The events will not fire if you override WndProc and set the result of a WM_NCHITTEST message. Windows doesn't even send out the corresponding mouse messages that would cause the .NET events.
My Solution
In my constructor, I had forgotten to call InitializeComponent().
Which was where the event handlers were being bound to my controls.
Events were not firing because the handlers were not being bound.
Are you sure that your form has focus? If your form does not have focus, the mouse events will not be fired.

Windows Forms HelpButton Changes Cursor

I have a HelpButton on a Windows Forms application. When clicked, I just want it to show a message box. This is working fine...
private void Form1_HelpButtonClicked(object sender, EventArgs e)
{
MessageBox.Show("This is an awesome program", "Awesome Program");
}
The problem is, when the user closes out of the message box, the form cursor is switched to the "Help" cursor with the question mark on it. This is not good. How do I prevent the cursor from changing? I tried putting this.Cursor = Cursors.Default and this.Cursor = Cursors.Arrow after the message box call, but it was ineffective.
Your event handler declaration is incorrect, the e argument is actually of type CancelEventArgs. Now it is simple:
private void Form1_HelpButtonClicked(object sender, CancelEventArgs e) {
MessageBox.Show("This is a more awesome program", "Awesome Program");
e.Cancel = true;
}
You want to handle the HelpRequested event and set the Handled property of the event args to true, and don't bother handling the HelpButtonClicked event.
private void Form1_HelpRequested(object sender, HelpEventArgs hlpevent)
{
MessageBox.Show("This is an awesome program", "Awesome Program");
hlpevent.Handled = true;
}
Try changing the Cursor.Current static property.

Creating A Continuous Action During A Windows Form Mouse Event

When I put a button on a form in C#, Visual Studio 2005, and have an action triggered by a button event, such as MouseHover or MouseDown, then the event triggers a single call to the function which defines the action despite the fact that I may continue to hover or keep the left button down. In this case I am trying to move a graphical object by rotating or translating it. I don't want to continue to click the mouse in order to get a repeated call to the transforming function, just keep the mouse hovering or hold the button down. What maintains the action until I cease my own action?
Set a flag on MouseEnter and keep doing the action while the flag remains true. Set the flag to false on MouseLeave.
In your case you need to use a combination of the events MouseDown, MouseMove and MouseUp.
Here a small simplified example to start:
private void OnMouseDown(object sender, EventArgs e)
{
//hit test to check if the mouse pointer is on a graphical object
_myHitObject = the_selected_object
}
private void OnMouseMove(object sender, EventArgs e)
{
if(_myHitObject != null)
//do your action relative to the mouse movements.
}
private void OnMouseUp(object sender, EventArgs e)
{
_myHitObject = null;
}
The solution is to use DoEvents() which allows for the MouseLeave event to be noted and the class variable "more" to be changed:
private void MouseEnter_ZoomIn(object sender, EventArgs e)
{
more = true;
while (more == true)
{
c1Chart3D1.ChartArea.View.ViewportScale *= ZoomMultiple;
Application.DoEvents();
}
} // MOUSEENTER_ZOOMIN()
//-------------------------------------
private void MouseLeave_Stop(object sender, EventArgs e)
{
more = false;
}

Categories