Click on form does not click underlying control - c#

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...

Related

What is the event which fires every time when i click outside for my wpf page?

i am developing windows application using wpf. I want to do some functionality when i click outside of my control created. for example, if i have Message-box open in my window, i want to do some function if i click outside of my Message-box window.
I tried,
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
.....
}
but its not working.. please any one tell me, what is the event fire when i click outside of my control?
you have two supposed solutions:
one of them is to get Mouse.X, Mouse.Y from System not from application, this article will help
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
second which is better is not using Dialog, but use PopUp window and this article will help
how to close a WPF Dialog Window when the user clicks outside it

Deactiavte event seems to consume mouse click

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

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.

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();
}

How to do two things with one click in Windows Form

On my main form, there is another (floatable) window. This floatable window works sort of like a popupwindow in that it will close when the user clicks somewhere else outside of this window. This is handled by the Deactivate event. But what I want to do is, if the user clicks on a different control (say a button), I want to both close this float window and then activate that button with just one click. Currently, the user has to click twice (one to deactivate the window and once more to activate the desired button). Is there a way to do this with just one click?
foreach(Control c in parentForm.Controls)
{
c.Click += delegate(object sender, EventArgs e)
{
if(floatyWindow != null && floatyWindow.IsFloating)
{
floatyWindow.Close();
}
};
}
And then add your handlers as normal. This additional handler can close the floaty window.
Make sure you floaty window isn't a dialog too as this will not allow your parent form's controls to be clicked.
I had a slightly hacky solution. In your Deactivate event, fire another custom event to your main form. Then when you main form is handling the custom event, enumerate through your control(this.Controls) and locate the control under the mouse by checking all their bound then call Focus(). You might need to sort by the one with the smallest surface area, or you can have a separate list of "focus-able" control like button just for this purpose.
Another way might be to switch focus to your main form immediately after OnMouseLeave of the floatable window, or OnMouseHover of your main window, but keep the floatable windows on top, just no focus. Handle the global mouse down of your main form, and close the floatable window by then.
These are just theories, not tested.
I had an issue like this once too, when a customer wanted "floaty" windows all over there application. I used used an approach similar to the one described in this article:
http://www.vbaccelerator.com/home/NET/Code/Controls/Popup_Windows/Popup_Windows/article.asp
Code sample available here:
http://www.vbaccelerator.com/home/NET/Code/Controls/Popup_Windows/Popup_Windows/Popup_Form_Demonstration.asp
By extending this a bit we created "floaty" windows similar to the ones VS uses when you get a runtime error while debugging code.
At the very least reading the code may give you some insight, however, quarrelsome's response may be the more simple solution.

Categories