C# WinApps: the main form has a key binding to CTRL-V ...so anywhere in the main app when I press CTRL-V something runs..good ... but also there are some MDI apps that are opening inside this main app ... in one of those there is a test box...ah! now CTRL-V also has a meanining for text box which is "Paste" ... so I added PreViewKeyDown to textbox and handled it, so now it is pasting BUT it is ALSO doing the main CTRL-V key binding that I had defined for the whole app ... but I do no want this to happen.... what can I do? ( I cannot change the key binding od the main app. I must keep it.)
You need to register for the Control.KeyDown event and mark it as handled to prevent further propagation of the event.
e.Handled = true;
Related
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.
I have a bug in my application that I don't know how to how to attack. Basically, key events "go missing" after some time. I have added event handlers to KeyDown, KeyPressed and KeyUp that write to the console (both for the Form and one of its TextBoxes). Here's the correct order of things:
control_PreviewKeyDown
ShellForm_KeyDown
control_KeyDown
ShellForm_KeyPress
control_KeyPress
ShellForm_KeyUp
control_KeyUp
In the failed state (which I can't reproduce consistently), I get these events when pressing a key (TextBox has focus):
ShellForm_KeyUp
control_KeyUp
Form.KeyPreview is true.
It's the same for all TextBoxes in the Form; they are not disabled and not set to readonly, but it's no longer possible to write text.
Problem is, I don't really know where to begin.
Begin with Application.AddMessageFilter(...) just to intercept the flow of events.
You can find some ideas here.
I would like to remove the original event behavior of controls within a form (similar to design mode).
So, when the user clicks on the button, i only want to capture that event. I do not want the original button event to be fired. Is this somehow possible?
I am looking for a generic solution. So it should work with any form and any control within the form.
Reason: I wrote a form validation rules designer. It uses reflection to enumerate all form-types in the entry assembly. The user can then select a form type, the designer creates that form, enumerates the controls, and embedds the form in the designer panel.
clicking on a control, opens a formular designer panel, and the user can now create a formular for that control and saves the formular to a DB.
When the form is then opened in the normal "runtime" mode, it loads its validation formulars.
Events are not in fact disabled in the Winforms designer. The designer executes the constructor of the form through Reflection, everything in the InitializeComponent() method executes, including the event subscriptions. Wherever this might cause a problem, the controls check the DesignMode property (prevents a Timer from starting for example) or by custom designers. The form is displayed underneath a transparent layered window on top of which the selection rectangle and drag handles are painted. Which prevents issues with mouse clicks and keyboard focus.
You probably ought to look at this magazine article to get this working for you.
From what I understand from your question, I guess, you can still use the "DesignMode" property for this as well. In your event handling routine, you may want to bypass execution by checking on this property:
if (this.DesignMode) return;
as the first statement in your event handling block of code.
Is there any way to handle the Esc key event in Windows Forms?
Actually it is working fine when I put debugger in the code. Once I remove the debugger it is not getting fired.
What's the problem here?
If it is for a dialog then you can set the property CancelButton (of the form) to the button that cancels (e.g. named btnCancel and with text "Cancel" in a English language application.)
In this case you don't need to handle the key event for the Esc key.
you should add Form.KeyPreview = true in your Form.Designer.cs. And later you can use Form.KeyPress event.
I have a windows forms Form that has a menu bar that grabs Ctrl-C. Inside the form's copy handler is a switch statement calls the correct copy method depending on what kind of control is selected.
I have now added a WPF UserControl as one of the child controls. In the UserControl, is a TextBox. I would like to have Ctrl-C activate the TextBox's Copy command. What is the easiest way to launch that command? Or maybe there is an easy way to fire a keypress event on the usercontrol?
I am not very much sure about how to send the KeyPress event on usercontrol, but for the textbox copy you can fire ApplicationCommands.Copy command and WPF textbox automatically handle the copy command for it, if focus at TextBox.
ApplicationCommands.Copy.Execute(null, null); inside your copy handler if user control is selected.