How can I check that whether mouse pointer is pointing a button or some other control?
I want to perform a particular task when mouse hover/move a button.
I know I can set event on individual button. But isn't it possible to check the pointed/hover control is button?
The sender argument in an event method should have the information you need...
private void MyEventHandler(object sender, EventArgs args) {
if(sender is Button) {
//Do some stuff
}
}
I'm not sure if you mean: can I do this without event handlers for MouseHover in individual controls. If so, the answer is no.
But you can attach each contol's MouseHover event to just one event handler that could look like the one in Chris's answer. For convenience you could even do that programmatically by looping through the controls in the form's load event. (assuming this is winforms)
Related
I developping one win-form application which having one custom control with one label and text box, and placed the custom control in one panel with docksytle as fill,
there is mouse click event for panel and custom control both, but when i click only custom control mouse click event is firing not the panel click event,
so anyone please let me know how to call the panel mouse click event.
Are you sure that you really need to invoke click of parent control? In general it would be, in my opinion, a code smell if you will do something like that - especially when it requires some strange constructions.
If you need to react in a same way when clicking on panel and on any child control inside the panel, it should be enough just to call the same method from two event handlers (that is from event handler of parent panel and event handler of child control. If you need, for example, mouse pointer location inside parent panel, you can easily calculate the position of mouse pointer using, for example, PointToScreen() and PointToClient() methods.
This is not a general solution, but maybe it's what you're looking for:
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
panel_MouseClick(sender, e);
}
private void panel_MouseClick(object sender, MouseEventArgs e)
{
}
Create Click Event for each control in panel and invoke the parent :
private void This_Click(object sender, EventArgs e)
{
this.InvokeOnClick(this, null);
}
This is my first question so please go easy :)
I am new to WPF and Desktop based applications and I am studying Event Handling. Going through Bubbling and Tunneling I can not find an example anywhere that explains how to use tunneling on a Button_Click.
Basically when I click a button I need the parent control (in this case a grid) to handle the event first and do some checks before allowing the Button_Click to take place. The problem I am having is I can use the Grid_PreviewMouseDown to capture the event but this is ambiguous! It does not tell me (at least i think it doesnt) what control caused the handler to trigger.
What can i do to determine the PreviewMouseDown was triggered by a Button Click? Or:
Is there an alternative/better was to tunnel a Button_Click?
Thanks
In your handler, you should inspect the Source of the event to get the control which initiated it. Just note that it is not readonly and could be changed so the Source refers to a different control.
You'll probably have better luck registering with the PreviewMouseLeftButtonDown event to get left clicks and not just any click.
If your handler is meant to only look for the left mouse click, you could use this code:
private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Button button = e.Source as Button;
if (button != null)
{
// button is being clicked, handle it
}
}
I'm implementing copy-paste in a Windows Forms application.
I need to enable/disable the bar-buttons for this two operations when the user changes the focused element in the application.
I can find the current focused control using something like this: http://www.syncfusion.com/FAQ/windowsforms/faq_c41c.aspx#q1021q, but how can I detect that the focused control has changed?
In your form load event handler you could also loop through all of the controls contained in the form and for each focusable control add an event handler for the Enter event:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
control.Enter += ControlReceivedFocus;
}
}
void ControlReceivedFocus(object sender, EventArgs e)
{
Debug.WriteLine(sender + " received focus.");
}
My proposal is to use Application.Idle event.
Write logic that enables/disables your buttons in Application.Idle event.
Subscribe to Application.Idle event on form shown event
Check button availability on button click (so you never pass accidental click under heavy load)
Do not forget to remove Idle handler on form disposing (or closing), because this is static event
Using this technique you will always have correct buttons state, and you not need to worry about subscribing to many controls events to detect focus change. This is also light-weight approach, because Idle event is raised only when application is not busy.
I think you should add an event handler to the control (or if you have many of the same type, subclass it, and override the appropriate OnChange handler). This way you won't have to 'find' the focused control (it will be given as the sender parameter), and the event will only arise when the change actually happened.
To detect the focus on a control you can create this event:
void MyGotFocus(object sender, EventArgs e)
{
if (sender is TextBox)
{
//TODO YOUR OPERATION
//FOR EXAMPLE
(sender as TextBox).SelectAll();
}
}
and the next step is to associate the control and event by code:
myText1.GotFocus += MyGotFocus;
myText2.GotFocus += MyGotFocus;
I'm currently developing a custom control and realize that my code is being run twice. It is not really a huge issue (it is only a Focus method call). However, I would like to understand it.
From reading the MSDN description for click | onclick event, it states that:
Fires when the user clicks the left mouse button on the object.
So I added the OnClick event and the MouseClick events to handle both left and right clicking. But after debugging the code I found that the OnClick handles both left and right click events.
Why is OnClick handling both and do I need to keep both events in my code for some reason I'm overlooking?
protected override void OnClick(EventArgs e)
{
this.Focus();
base.OnClick(e);
}
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
rightClickMenu(e);
}
}
According to MSDN, the Click event is called not only when the mouse is clicked, but also when the Enter button is pressed. If you only need to handle mouse clicks, I'd move all of your code in the MouseClick event. You can't do it the other way around because the Click event doesn't tell you which mouse button (if any) was clicked.
First of all, your link is incorrect, it links to HTML and DHTML Reference, not WinForms :)
Correct link is Control.MouseClick event
You need to override only one method. If you want to handle only mouse clicks - override OnMouseClick() and don't handle MouseClick event, otherwise - override OnClick() and don't override OnMouseClick().
You shouldn't need to have both events... Just keep the OnClick.
Also, I haven't done Windows Forms in quite a while, but I think there's a better way to accept focus than manually setting it on the click event, but I can't tell you specifically what it is... I think there's a property for it or something.
In Winforms, the Click event is raised when either mouse key is clicked.
If my memory serves me right, click does both mouseclick and the 'Enter' key or even setting focus on the control using the 'Tab' key and then using 'Space' or 'Enter' to "click" it.
If such behaviour is acceptable/desired, you may do the following.
I had this workaround for a DoubleClick event...
void ControlClick(object sender, EventArgs e)
{
MouseEventArgs mEvt=e as MouseEventArgs; // or (MouseEventArgs)e;
// now mEvt has the same properties as 'e' in MouseClick event
}
Hope this helps.
-Nurchi
The OnClick and CustomControl_MouseClick is the same event
You can have how many methods you want attached to an event ( this.Click += ...)
I have a bunch of controls (textbox and combobox) on a form with toolstripcontainer and toolstripbuttons for save, cancel etc for edits. We are using .Net 3.5 SP1
There is bunch of logic written in control.lostfocus and control.leave events. These events are not being called when clicked on the toolstrip buttons. Is there a way to call these events manually when any of these buttons are pressed.
Thanks.
Kishore
[Edit]
This is how I solved the problem. Thanks Chris Marasti-Georg for the pointer. In the button click event I calling focus on the toolstrip instead of the button as the toolstripbutton does not have a focus event. We can access the toolstrip on which the button is placed using
((ToolStripButton)sender).Owner.Focus()
-Kishore
You could listen to the click events on the buttons, and in the handler call their focus method. That would (hopefully) cause the previously focused control to respond correctly. Add the following handler to each button's click event:
private void ButtonClick(object sender, EventArgs e) {
if(sender != null) {
sender.Focus();
}
}
You can extend those controls and then call the OnLostFocus and OnLeave protected methods of the base class...
I'd suggest moving the login to a method outside the event handler and calling that method...