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.
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 custom ComboBox which opens a separate control when you click on the arrow of the ComboBox. I would like to call the 'LostFocus' event handler to close the custom control when you lose focus of the ComboBox.
This works fine if you click onto another control such as a text box, however doesn't fire if you click on the background of the form.
I want to mimic the functionality of when you click off and close the dropdown of a normal combobox.
Take a bool variable at Form level and make it true during combobox enter(GotFocus) event. InLeave(LostFocus)event ofcombobox, make itfalse`.
Subscribe to Form MouseClick event and check bool variable in this event. if true , call combobox leave event here.
I'm new to Visual Studio and am converting a C# console app to VS so that I can give it a GUI. The GUI piece is definitely a learning curve.
I have a main Form1 with Tab1 and Tab2. I can call this.AcceptButton, but it only appears to be at the form level. Is there a way for each tab to have an AcceptButton? I can't call this.Tab1.AcceptButton, however when I am in Tab2, the function of AcceptButton doesn't seem to trigger. Is it because the button doesn't exist on the tab that is in focus?
I could certainly forgo using AcceptButton if there's not a clean way to do this, but it would increase the usability of the application.
Thanks in advance!
You can listen to SelectedIndexChanged event on your TabControl. So whenever tab is change this event will fire and you can use assign another button to AcceptButton.
Have a look at this: Change accept button with tabs
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.
I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}