This question already has answers here:
Panel not getting focus
(6 answers)
Closed 7 years ago.
I have got some controls on the Panel and I am trying to delete them using "Delete" button. I handled KeyPress Event as mentioned in How to get Keypress event in Windows Panel control in C#
and I am getting Event for buttons (A-Z and 1-9) pressed, but not for the Delete, Control/Alt/ Shift or F1, F2.... buttons.
Do we need to do something special for handling these buttons?
Try like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
e.Handled = true;
}
}
Also you need to set KeyPreview on.
You can also refer Keyboard.Modifiers Property
Related
This question already has an answer here:
C# disable textbox focus on form load
(1 answer)
Closed 3 years ago.
i have a application where
Scenario :
when i click on that textbox the cursor should not point the textbox it should be disabled
how do i achieve this disabling the textbox
textbox1.focus()=false;
textbox1.focused()=false;
You can set this.ActiveControl = null; with this is your Form
This code no control is active and your textbox lost focus too.
private void YourForm_Load(object sender, EventArgs e)
{
this.ActiveControl = null;
}
To disable a textbox:
textBox.IsEnabled = false;
To not focus the textBox just focus on an other element.
For example on a Forms class:
this.Focus();
This question already has answers here:
Parent Control Mouse Enter/Leave Events With Child Controls
(6 answers)
Closed 4 years ago.
I am currently working on a subclass to the WinForms NumericUpDown Control. But because they built it so it contains multiple sub-controls, it messes up the MouseLeave and MouseEnter event.
When the mouse moves into the controls bounds, MouseEnter will fire followed by MouseExit when the mouse moves over one of the subcontrols (e.g. the Up and Down Arrow Buttons). In addition to that, MouseExit will not fire at all sometimes, especially likely when moving the mouse out of control bounds fast...
How can I get the events to fire properly or work around this problem?
EDIT:
Here's a simple code example:
public class TMNumericUpDownControl : NumericUpDown
{
public TMNumericUpDownControl()
{
MouseEnter += MouseEnterHandler;
MouseLeave += MouseLeaveHandler;
}
private void MouseEnterHandler(object sender, EventArgs e)
{
Debug.WriteLine("entered");
}
private void MouseLeaveHandler(object sender, EventArgs e)
{
Debug.WriteLine("Left");
}
}
Okay I just got it myself.
I noticed I can just access the NumericUpDowns child controls via NumericUpDownName.Controls and assign the event handlers to them as well. Thought this wouldn't be possible before...
That means this is in fact a duplicate Question.
This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I would need that (on a WinForm) if the key A is pressed, an event is triggered.
I got this code from the MSDN site:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.A)
{
MessageBox.Show("Key 'a' was pressed.");
}
}
The problem is that this code doesn't work for me, there is no error message, but if I am on the form and press the key A there is no event triggered. I tried to use breakpoints, but it never enters the if.
What is the problem here?
You need to set Form.KeyPreview property to True. This property gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
Also note that there is a mistake in your code; according to your message you need to verify if e.KeyCode == Keys.A.
This question already has answers here:
How to set hotkeys for a Windows Forms form
(8 answers)
Closed 8 years ago.
Currently I want to add a hotkey to my button in C# windows application.
I went to the event, I didn't see anywhere I can assign hotkeys.
I wanna assign "F6' as the hotkey, which means whenever I press F6 it would trigger the button and run the code in it
You could try to handle the Forms Keypress-event (and route all components keypress-events to that)
Assigning hotkeys via the GUI works just for MenuItems, if I'm not mistaken.
Edit: some code:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F6)
button1_Click(this, new EventArgs());
}
Just what I quickly tried. If you register to the forms (or any other controls for that matter) KeyUp-event, you can query the released key and act accordingly. I suppose there are more elegant ways to do this, but this should get you started.
I would like to control the focus of my winform application. It is made of a custom listbox and several other component.
I want all the keyboard event be managed by my window handlers in order to avoid specific control key handling (for example when I press a character and the list box is focused, the item starting with the correspondant letter is selected which is not a correct behaviour for my application).
How can I achieve this?
Make sure your form's KeyPreview property is set to true. Then this code should work for canceling your key events to the listbox...
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.ActiveControl == listBox1)
e.Handled = true;
}
The KeyPress event may not work for all your scenarios. In that case, I would try out the KeyDown event.