WPF: Is this behavior intended? PreviewLostKeyboardFocus and LostKeyboardFocus - c#

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.

Related

Prevent Click events on child view while swiping(flicking) parent. WinPhone8.1

I have canvas with listbox inside it.
each child element of listbox sets eventhandler for Click event.
On canvas I set eventhandlers for
ManipulationStarted="canvas_ManipulationStarted"
ManipulationDelta="canvas_ManipulationDelta"
ManipulationCompleted="canvas_ManipulationCompleted"
My code for swiping works perfect accept one thing, it fires Click eventhandler before ManipulationCompleted eventhandler.
But for example listbox in the same time scrolls perfectly and do not fire Click event.
So basically what I need is to handle manipulation events in same way listbox do.
If this condition is true:
private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.DeltaManipulation.Translation.X > [some value]
....
}
I need to disable firing Click event on any child element of canvas, doesn't matter if it is inside listbox or not.
Why are you setting click handlers if you don't want them to fire?
Click fires on pointer pressed, so there's no way to tell if the user wanted to Click or to start a manipulation. You'll need to either decide based on the location clicked or a later event if you want to differentiate between "clicks" and swipes.
Instead of Click you can handle the Tap gesture along with the manipulation events. Since Tap fires on the pointer released the manipulation system will fire it if the user tap and releases in one spot and it will trigger the manipulations if the user presses and moves the pointer.
See How to handle manipulation events for Windows Phone 8 for more details.

Keydown event not firing in user control obj

I have a user control which is not declared in the designer. I have a button that I want to have create this user control when I click it - it should initialize the user control and insert it in the main UI.
However, it happens that the user control has a key press event on it, which is not firing.
Why does this happen?
I already tried to attach the event on the user control itself but it seems that it's not firing at all. Is there some kind of bug?
It is very hard to fix problems with code that you can't see, but in WPF, there are often reasons why Bubbling events like the KeyDown event don't fire. Occasionally certain controls may make use of these events and set them as handled internally, thereby stopping them from bubbling up any further.
The normal solution on these occasions is to use the related Tunneling events instead, which are raised before the Bubbling methods and not used internally by controls. So, while I can't guarantee that this will fix your problem, it is certainly worth trying to handle the UIElement.PreviewKeyUp event instead of the UIElement.KeyUp event.

Redirecting keyboard activity to a specific control

I have a C# WinForm application. On the main form, I have a user control. What I want to be able to do is, whenever a key is pressed on the keyboard, I would like to have my user control receive the keyboard input, so that the keyboard related events (KeyDown, KeyUp and KeyPress) all fire inside the one specific user control.
I would like the actual main form and any other user control on the form to ignore the keypress. Is this possible?
You can do this by ensuring that your control always has focus. An easy way is in the Control.LostFocus event set the focus back. The UserControls base class isn't great about recieving focus, so instead you may want to pick a control within your user control to always have focus and receive events.

C#: custom control calls click handler every other click (winform, not ASP)

I have written a custom control in C# (inherited from Forms.Control) and it seems to working fine, but if you press the button fast enough a problem occurs: only every other click will call the click event handler. This doesn't happen if you don't press it fast (less than once a second). The mouseUp and mouseDown handlers always get called no matter how fast you click the button.
Of course doesn't happen with the canned winform button.
I cannot use the canned button because I'm writing an application for the .net compact framework, so I need a custom control in order to make the UI more presentable. Also, I tested out my code on the full version of the .net framework, and I still have the same problem.
Any help would be greatly appreciated. Thank you!
If you are clicking rapidly enough, you are getting into DoubleClick territory.
According to above MSDN Page the order of events are:
The following series of events is raised by the control when such a user action takes place:
MouseDown event.
Click event.
MouseClick event.
MouseUp event.
MouseDown event.
DoubleClick event.
MouseDoubleClick event.
MouseUp event
If you will notice there is only one Click event per DoubleClick
For a way to disable it try looking at this MSDN Page discussing ControlStyles.
From above link:
StandardClick -- If true, the control implements the standard Click behavior.
StandardDoubleClick -- If true, the control implements the standard DoubleClick behavior. This style is ignored if the StandardClick bit is not set to true.
So try this in your controls constructor or load event:
this.SetStyle(ControlStyles.StandardClick, true );
this.SetStyle(ControlStyles.StandardDoubleClick, false);
Since SetStyle does not appear to be in the Compact Framework you could add a DoublClick Event and have it trigger the Click event Programmically like this.
YourClickEvent(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left,1,0,0,0));
When you click your control fast enough, it calls double click rather than click.
So, you should do something like this in your click function:
{
control.Enabled = false;
......
control.Enabled = true;
}

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

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.

Categories

Resources