Detect navigation key press in datagridview - c#

I tried with keypress event of datagridview, but it was not working. How can I detect the up-down-left-right arrow key presses in datagridview?

Try with KeyDown event:
private void dgv1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
//do stuff
}
}

It is possible with PreviewKeyDown:
public Form1()
{
InitializeComponent();
dgv.Columns.Add(new DataGridViewTextBoxColumn());
dgv.Rows.Add("text");
dgv.PreviewKeyDown += (sender, args) =>
{
Debug.Print(args.KeyCode.ToString());
};
}

Related

form keydown event interrupt the working of textBox keypress event c#

I have a form, where I am using Form_keyDown Event and KeyPreview=true.
The code is as follows
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
button2_Click(sender, e);
if (e.KeyCode == Keys.P)
button3_Click(sender, e);
if (e.KeyCode == Keys.B)
button4_Click(sender, e);
if (e.KeyCode == Keys.U)
button5_Click(sender, e);
if (e.KeyCode == Keys.T)
button6_Click(sender, e);
if (e.KeyCode == Keys.W)
button7_Click(sender, e);
if (e.KeyCode == Keys.X)
button8_Click(sender, e);
}
But when I press the keys (i.e. C,P,B,U,T,W and X) on a textBox the event fires. What I need is the Skip the TextBox from Form_keyDown event. Is this possible?
The Function keys are already in use and the user does not prefer to user alt or ctrl.
Is there is any away to form_keyDown event and textBox KeyPress works differently?
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (ActiveControl.Name!="textBox20")
{
if (e.KeyCode == Keys.C)
button2_Click(sender, e);
if (e.KeyCode == Keys.P)
button3_Click(sender, e);
if (e.KeyCode == Keys.B)
button4_Click(sender, e);
if (e.KeyCode == Keys.U)
button5_Click(sender, e);
if (e.KeyCode == Keys.T)
button6_Click(sender, e);
if (e.KeyCode == Keys.W)
button7_Click(sender, e);
if (e.KeyCode == Keys.X)
button8_Click(sender, e);
}
}

C# - How can I block the typing of a letter on a key press?

I have a textbox with a OnKeyPress event. In this textbox I wish to input only numbers, and for some specific letters like t or m, I would want to execute a code without that letter being typed in the textbox. Small sample of what I am trying to do:
//OnKeyPressed:
void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.T || e.KeyCode == Keys.M) Button1Click(this, EventArgs.Empty);
}
This unfortunately does not prevent the input of the letter..
Set the SuppressKeyPress property from KeyEventArgs to true, like below:
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction.
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
int i;
string s = string.Empty;
s += (char)e.KeyValue;
if (!(int.TryParse(s, out i)))
{
e.SuppressKeyPress = true;
}
else if(e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}

Detect combined keys pressed

I want to detect if the user clicked few keys together (example: ctrl+c , alt +F4 etc..)
How can I do it??
I googled it but I got confused... :/
I tried this:
if (Control.ModifierKeys == (Keys.C) &&Control.ModifierKeys == Keys.Control)
{
MessageBox.Show("Test");
}
But it didn't worked,any ideas?
I just understood what I was should to do:
I was needed to type this:
void detectCopy(object sender, KeyEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
{
//work here
}
}
and this on the constructor:
public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(detectCopy);
}
Your if statement is incorrent use this:
if (e.KeyCode == Keys.C && Control.ModifierKeys == Keys.Control)
{
}
On the constructor do this:
public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(detectCopy);
KeyPreview = true;
}
And in the code do this:
void detectCopy(object sender, KeyEventArgs e)
{
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
{
//work here
}
}
In Windows Form, the Propertie KeyPreview need be true to KeyDown Event works!!!

Handle the KeyDown Event when ALT+KEY is Pressed

How do you handle a KeyDown event when the ALT key is pressed simultaneously with another key in .NET?
The KeyEventArgs class defines several properties for key modifiers - Alt is one of them and will evaluate to true if the alt key is pressed.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyData != (Keys.RButton | Keys.ShiftKey | Keys.Alt))
{
// ...
}
}
Something like:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt)
{
e.Handled = true;
// ,,,
}
}
This is the code that finally Works
if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z && e.Alt){
//Do SomeThing
}
I capture the alt and down or up arrow key to increment the value of a numericUpDown control. (I use the alt key + down/up key because this form also has a datagridview and I want down/up keys to act normally on that control.)
private void frmAlzCalEdit_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.Down)
{
if (nudAlz.Value > nudAlz.Minimum) nudAlz.Value--;
}
if (e.Alt && e.KeyCode == Keys.Up)
{
if (nudAlz.Value < nudAlz.Maximum) nudAlz.Value++;
}
}
Create a KeyUp event for your Form or use a library like I did to get a GlobalHook so you can press these keys outside the form.
Example:
private void m_KeyboardHooks_KeyUp(object sender, KeyEventArgs e)
{
if ( e.KeyCode == Keys.Alt || e.KeyCode == Keys.X)
{
}
}

How to disable navigation on WinForm with arrows in C#?

I need to disable changing focus with arrows on form. Is there an easy way how to do it?
Thank you
Something along the lines of:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
}
}
void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
e.IsInputKey = true;
}
}
I've ended up with the code below which set the feature to EVERY control on form:
(The code is based on the one from andynormancx)
private void Form1_Load(object sender, EventArgs e)
{
SetFeatureToAllControls(this.Controls);
}
private void SetFeatureToAllControls(Control.ControlCollection cc)
{
if (cc != null)
{
foreach (Control control in cc)
{
control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
SetFeatureToAllControls(control.Controls);
}
}
}
void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
e.IsInputKey = true;
}
}
I tried this aproach, where the form handles the preview event once. It generates less code than the other options.
Just add this method to the PreviewKeyDown event of your form, and set the KeyPreview property to true.
private void form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
e.IsInputKey = true;
break;
default:
break;
}
}
You should set KeyPreview to true on the form. Handle the KeyDown/KeyUp/KeyPress event and set the e.Handled in the eventhandler to true for the keys you want to be ignored.

Categories