C# - preparing event after pushing two buttons on keyboard [duplicate] - c#

This question already has answers here:
How to use multiple modifier keys in C#
(9 answers)
Closed 8 years ago.
i've got problem. If I push one key I can get event for example:
if (e.KeyCode == Keys.F4)
{
Method();
}
How could I do the same if I push two keys? For example Enter + F4?

FormLoad()
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
//Works for Ctrl+F4
if (e.Control && e.KeyCode == Keys.F4)
{
//Do something
}
}
See if this work for you.

Related

windows form keyup event is not working c# [duplicate]

This question already has an answer here:
keydown in c# doesn't work for some reason
(1 answer)
Closed 5 years ago.
This code is not working in my main form
void main_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
e.SuppressKeyPress = true;
}
}
Try to set Form property: main.KeyPreview = true;

C# WPF How to use only numeric value in a textbox [duplicate]

This question already has answers here:
How do I get a TextBox to only accept numeric input in WPF?
(33 answers)
Closed 5 years ago.
I use a WPF application I would like to allow only numbers in a textbox. In a WindowsForm application I would know how I would have to make it.
Unfortunately, there is a KeyPress not in WPF and the "code" functioned also no longer.
How is it right and what is the event?
Here you can see the old code that has worked in Windows Form:
private void tbKreisdurchmesser_KeyPress(object sender, KeyEventArgs e)
{
if (char.IsNumber(e.Key) || e.KeyChar==".")
{
}
else
{
e.Handled = e.KeyChar != (char)Keys.Back;
}
}
You can add Previewtextinput event for textbox and validate the value inside that event using Regex,
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
e.Handled = Regex.IsMatch(e.Text, "[^0-9]+");
}

how does Keys type work? [duplicate]

This question already has answers here:
Test if the Ctrl key is down using C#
(6 answers)
Closed 6 years ago.
I'm using Keys type to detect pressed keys.
At some place I do comparison like this:
if (keyData == Keys.Control)
//do something
where keyData is the key pressed of type Keys
However keyData contains
ControlKey | Control
So of course comparison doesn't work because Keys.Control contains only Control.
So what is the correct way to compare them?
This is how I do it:
private void mainImage_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl) // System.Windows.Input.Key
LeftCtrlButtonIsPressed = true; // raise a flag
}

C# - Hide window on input from user [duplicate]

This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I have put this code in my application, I want the window to hide when control + Q is clicked, why does this not work?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Q)
{
this.Hide();
}
}
You can check the Key modifier in order to handle Keys combination
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
{
//do stuff
}
}

An error in key Down function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am working with c# windows application and i need to enter some record to database after inserting in to textbox. I have tried this code
private void textBoxItemCode_KeyDown(object sender, EventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}
but i am getting an error near e.KeyCode so how to make it
You need to use the following args
System.Windows.Forms.KeyEventArgs
Your handler should look like this
private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}
That way you have access to the KeyCode property which does not exist on the base EventArgs
It Should be like this :-
private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}

Categories