Control's Focused property has changed as of Leave handler when using mouse, but not keyboard - c#

I am working with a TextBox, and need to fire some logic when the textbox has lost focus.
My problem is twofold:
The Leave event is firing on every keypress for some reason, meaning the logic is run with every keypress when it should not.
When using the Focused property of the Textbox as a double-check, simply exiting out if the property is still set, it now works when the user uses the mouse to leave, but not when the user tabs out.
The Focused property of the TextBox in question is False as of when its Leave event fires when the mouse is used to change focus, but it is still True when the Leave event fires due to a Tab keypress. Seriously?
I need a workaround, because the logic firing on every keystroke is causing a problem for users right now that needs to be fixed post-haste.

I created a form with a textbox on it and attached event handler to the leave event of that text box. I then typed a bunch of stuff into said textbox. The event was not raised. I hit tab, the event was raised. I then clicked back in the textbox, typed some more, and then clicked another control and the event was raised.
I'm just saying that something else is interfering with the textbox. I would look into that a little more, or post some code demonstrating the problem.

Related

How to remove focus from a button

I have a panel on which there is a button.But there is a problem. I need to use KeyEventHandler to catch the Enter press, the button is pressing, and the event is not processed. I tried artificially giving focus to Form, but it didn't help.If I don't add buttons, everything works fine. Can you tell me how to solve this?
I created a new project and tried: with the button, the Event is not called, and the button is pressed, without the button, the event is called.
you need to enable KeyPreview property of that form, to catch a KeyEventHandler. Also check that your AcceptButton property has correct value

How to change button click event into enter event?

I'm new on C#, I made a button which makes appear another button and I want to remove the click event of this new one and change it into an enter(key) event.
Code1
Code2
When the button appears it only works when I click it but no when I press Enter.
As to your question, https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.enter?view=net-5.0
The enter event is for when keyboard or mouse enter the control (button in this case).
From the sounds of your question, you are wanting to click the button when the 'Enter' key is pressed.
If this is WinForms, then you might actually want a property on the parent form called 'AcceptButton' (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.acceptbutton?view=net-5.0). This will make it so anytime the Enter key is hit (with the exception of controls such as a RichTextBox that accepts the return key) that button will be clicked (course you still need the button click event for the button).
Although you seem to be doing something weird in your example code image, so I'm not sure that's the correct solution for it. Instead, you need to look at maybe the Keyboard events, such as KeyPress (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keypress?view=net-5.0). There are several: KeyDown, KeyPress, KeyUp, and PreviewKeyDown.
Note these events require Keyboard focus on the control in question. So if add them to the button you are creating in your example code, then that button will need keyboard focus to receive the keyboard events.
Also, just FYI, if a button has keyboard focus, by default the SpaceBar will click that button for you, while the Enter key is generally reserved for clicking the Form's AcceptButton.

What is the exact cause that fires the validating event?

I have searched for the question that when is the validating event fired but the answer I found so far is that when the validation starts.
What I want to know is the actual firing time of the validating event.
For example, the click event is fired when a control is clicked by either mouse or keyboard and similarly leave is fired when a control is no more the current focused control.
So what is the explanation of validating event being fired?
The Validating event is fired only when the control that receives the focus has the CausesValidation property set to true. For example, if you have written code in TextBox's Validating event, and you click the OK button (CausesValidation = true) then the Validating event is raised, but if you click the Cancel button (CausesValidation = false) then the Validating event would not be able to fire.

WPF: Is this behavior intended? PreviewLostKeyboardFocus and LostKeyboardFocus

I have a TextBox and I want to save the content, when the user leaves the TextBox. I planned to use PreviewLostKeyboardFocus, but it doesn't work as intended.
<TextBox PreviewLostKeyboardFocus="textBox2_PreviewLostKeyboardFocus"
LostKeyboardFocus="textBox2_LostKeyboardFocus" />
When I click on another control inside of the same application, I first get the PreviewLostKeyboardFocus event and then the LostKeyboardFocus event. But when I activate another application, the PreviewLostKeyboardFocus event simply doesn't happen. I only get LostKeyboardFocus.
This is the expected behaviour.
The PreviewLostKeyboardFocus event is not raised when you switch to another application.
The main purpose of handling the event in the first place is to prevent the keyboard focus from changing: https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.previewlostkeyboardfocus(v=vs.110).aspx
And if the event was raised when you switch to another application, you would be able to prevent the keyboard focus from changing by handling this event and set the the Handled property of the KeyboardFocusChangedEventArgs to true and this would effectively prevent the user from being able to focus any other element on the screen while running your application.

How to get a button event to repeat fire when holding down said button

I want to be able to hold down a button (i.e. MouseLeftButtonDown) and have it's event repeat fire.
I code Mouse.Capture(button) in the MouseLeftButtonDown event and Mouse.Capture(null) in MouseLeftButtonUp event.
If I hold the mouse button down, the MouseLeftButtonDown only fires once. I can see this not by setting a breakpoint but by using Console.Writeline().
I had this very code working at some point. Then I refactored and put the mouse events in an Interface. Everything still works except this repeat action now!
Update - More information:
Here is the before call which was in a specific class:
Mouse.Capture(sender as MenuGelButton)
MenuGelButton is a xaml creation of ellipses and text to make a cool button.
Since adding the interface (which is common code for several xaml creations all of which inherit the interface), here is the current call from within the interface:
Mouse.Capture(sender as UIElement)
Update of Update:
A mouse click event is not designed to auto repeat fire. A keyboard event is. I had this working (and it continues to work) on a keyboard event not the mouse event. Sorry for the confusion...
I believe you are looking for Microsoft's RepeatButton. You can use it in XAML just like you would use a Button, and it fires a click event multiple times.
From their documentation:
A RepeatButton is a button that raises Click events repeatedly from the time it is pressed until it is released. Set the Delay property to specify the time that the RepeatButton waits after it is pressed before it starts repeating the click action. Set the Interval property to specify the time between repetitions of the click action. Times for both properties are specified in milliseconds.
Try doing something like this:
mouse button --> fires event
in the event, set a timer of your liking (how many times per second would you like the action to happen?)
in a loop, every time timer fires, check to see if mouse button is still "down". If yes, fire. If not, don't fire.
you'll probably want to add another event of mouse button up, that will stop the timer, so it won't fire anymore (or bind to a bool that will let you know if you want to fire ... up to you ..)
Hope it helps.

Categories

Resources