c# How to block Enter key? - c#

I have two buttons.
When I start my api and hit Enter then api doing code from buuton1.
How to block it?
private void frm2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
}...
Doesn't work.

You can block a key by setting SuppresKeyPress to true in your event handler frm2_KeyPress:
if (e.KeyCode == Keys.Enter) {
e.SuppressKeyPress = true;
}
Please note I have changed e.KeyChar to e.KeyCode as personally I find e.KeyCode == Keys.Enter more readible than (e.KeyChar == (char)13)

You can use the KeyPressEventargs.Handled property for this.
private void frm2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
e.Handled = true;
}
}
For more information, see: https://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled(v=vs.110).aspx

Related

keypress event for number in WinForm TextBox in C#

I want to limit user to type just numbers in TextBox.
I add this code In keypress Event:
private void txtPartID_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar >= '0') && (e.KeyChar <= '9')) == false)
{
e.Handled = true;
}
}
but after that BackSpace key don't work for this TextBox. How can I change this?
You can check for backspace using this,
if(e.KeyChar == '\b')
And better way to check only for numbers is
private void txtPartID_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
}
private void TxtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsDigit(e.KeyChar) && (e.KeyChar == (char)Keys.Back)))
e.Handled = true;
}
I think you should handle both back key and delete key.
if (!(Char.IsDigit(e.KeyChar) && (e.KeyChar == (char)Keys.Back)&& (e.KeyChar == (char)Keys.Delete)))
e.Handled = true;
You can use it
private void txtColumn_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar >= '0') && (e.KeyChar <= '9') || (e.KeyChar == (char)Keys.Back)) == false)
{
e.Handled = true;
}
}

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);
}
}

allow just number in a gridviewcell on editing

I have a gridview in my C# windows application ... It allowed to be edited and I want a special cell (named "Price") to just allow number on keypress ... I use the code below for texboxes to just allow numbers ... in which event of grid view should I write this code?
private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
(e.KeyChar != (char)(Keys.Delete) || e.KeyChar == Char.Parse(".")) &&
e.KeyChar != (char)(Keys.Back))
{
e.Handled = true;
}
}
You can use CellValidating event of DataGridView.
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
// Validate the Price entry.
if (dataGridView1.Columns[e.ColumnIndex].Name == "Price")
{
}
}
thx guys ... I used below code and my problem resolved ...
public Form1()
{
InitializeComponent();
MyDataGridViewInitializationMethod();
}
private void MyDataGridViewInitializationMethod()
{
gvFactorItems.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(gvFactorItems_EditingControlShowing);
}
private void gvFactorItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); ;
}
private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
(e.KeyChar != (char)(Keys.Delete) || e.KeyChar == Char.Parse(".")) &&
e.KeyChar != (char)(Keys.Back))
{
e.Handled = true;
}
}
I think you should take a look at this, it will help :-
DataGridView keydown event not working in C#

How can you display textbox1 value in textbox2 using keystokes

I have 2 textbox (textbox 1 & textbox 2 ); the textbox1 has numeric value such as $1,000. Now when i am in textbox2 and if i hit keystrokes like (= or + or pageup or pagedown etc...) the textbox1 value should appear in textbox2.
The reason behind this is to enable customers improve speed in processsing/ filling data.
I don't know if I understand what you need, anyway try this:
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown ||
e.KeyCode == Keys.PageUp ||
e.KeyCode == Keys.Oemplus ||
e.KeyCode == Keys.Add ||
(e.KeyCode == Keys.D0 && e.Shift))
{
textBox2.Text = textBox1.Text;
e.Handled = true;
e.SuppressKeyPress = true;
}
}
How about adding a keyPress event on textbox2 with a function like this:
private void textbox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '+')
{
textbox2.Text = textbox1.Text;
}
}
You can try this:
protected void textbox2_TextChanged(object sender, EventArgs e)
{
if(textbox2.Text=="+"||textbox2.Text=="=")
textbox2.Text=textbox1.Text;
}

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