Deactiavte event seems to consume mouse click - c#

I have nonmodal form that I display using
myform.Show()
I close the form whenever the user clicks somewhere outside of the form. I do this successfully by handling the Deactivate event on the form. Done as so:
private void myform_Deactivate(object sender, EventArgs e) {
this.Close();
}
I have a custom calendar underneath this form. I want to be able for the user to click on another day in calendar and the popup form go away automatically. Currently when the Deactivate event is called, the mouse click seems to be consumed. That is, the underlying calendar control does not receive the mouse click. Now the user has to click once to deactivate (close) the form and then another select a day. I'd like to do this all in one click.
I was hoping to be able to do something like
e.handled = false
in my Deactiavte handler but this of course is not an option. Help anyone?

The Mouse event is part of myForm not the other form where you calender in, if you need this event to trigger on the other form prior to this.close() you would call a method on your calender form forcing a mouse click event

Related

Activate Click and MouseDown events at the same time?

I am trying to create a form where the user selects one of 2 radio buttons, "fast" and "slow", then presses a "go" button on the form. It should work as follows:
When "fast" is selected and "go" is pressed, the user needs to continue holding down the button in order to make the player move and the player stops when the user releases their finger from the button. For this, I am using the MouseDown and MouseUp events.
If "slow" is selected, the user can only move one step at a time, regardless of how long the button is held down. for this, I am using the Click event.
To test this concept, I put a MessageBox in the Click and MouseDown events to see how they work together. When I click on the button, however, I am only seeing the Mousedown event get triggered, regardless of which radio button is selected and the Click event is only triggered when the go button is in focus and I press enter.
How can I make it so that both events get called simultaneously (and then I can put the respective if statement in to differentiate between "fast" and "slow" radio buttons)? (The button is wired up to these events by double-clicking on their respective handler in the design window, not hardcoded).
private void go_button_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mousedown"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("Mosuedown");
}
private void go_button_Click(object sender, EventArgs e)
{
Console.Out.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
Console.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("go_buttonclick");
}
I found what I was doing wrong, both events are being triggered at the same time, but the MessageBox in the MouseDown event wasn't allowing me to see the Click and MouseUp events also being triggered. When I turned off the MouseDown MessageBox, the other 2 MessageBoxes appeared, thanks!

Close form on deactivate breaks click event

I have a windows form in c# which should close when the user clicks anywhere outside of its bounds, eg:
form1.Deactivate += (o, e) => form1.Close();
My problem is, I would also like to catch the click event that caused the form to deactivate, but using the above code my mouse event handlers on the other forms in my application are never called.
For example, I have a click handler on a second visible form in my application:
form2.MouseClick += OnForm2Click
Normally OnForm2Click would get called fine if the user clicks on form2 when form1 is active, but with close-on-deactivate code above, OnForm2Click never gets called (this is a bit strange to me because double-click handlers are called just fine).
I cannot call form1.Close() from within OnForm2Click() as a solution because form2 has no reference to form1.
Thank you in advance for your solution.
You may solve your problem with a mouse hook. See:
Hooks Overview (Windows)

C# mouse click event after focus in windows 2012

I find a strange bug in windows 2012. I have a simple window (WinForm) with a text box and a button (textBox1 and button1).And I try to focus on textbox1, after form appear.
private void Find_Paint(object sender, PaintEventArgs e)
{
textBox1.Focus();
}
And if I set it Click and MouseClick events stop working. So I can't click on button.
In windows 2008 it's work. If comment focus line - works too.
Who can suggest a solution or perhaps an alternative? Need to get the cursor in the textbox after the form has appeared
You should use the Shown event instead:
private void Find_Shown(object sender, EventArgs e){
textBox1.Focus();
}
Note: you used Paint event which will be very nasty, everytime your form is repainted, your textBox1 will be focused, the Paint event is fired every time your form resizes, state changes, ... we can't determine exactly the time it fires but it fires fairly frequently when your form is running. That is the reason why you can't click on and select anything on your form. That's because clicking or selecting controls fires the Paint event and makes your textBox1 focused then.

Click on form does not click underlying control

I have a windows forms application. When I click on a window this activates the form and then I need to click again to call the particular control click event. For example if I click on a button this activates the form and then I need to click the button again.
Is there a way to perform the control click and window activation in one click? Preferably I would want this to work with whatever the clickable control is (menu,button, label etc)
So far I have managed to activate the win form on mouse over and then the control click works. I would like to have the win form activated on click and also run the click command on an underlying control if this has a click event.
Well, here's a way to accomplish what You want (just attach a similar method to Your from's MouseClick event):
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
var c = this.GetChildAtPoint(e.Location);
this.InvokeOnClick(c, e);
}
This method has it's drawbacks though - for example the control will be clicked even though it's disabled etc., so You have to make sure the control under the cursor is "clickable" by yourself...

How to bypass the enter/leave event in c sharp

I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}

Categories