Key Down with Dollar - c#

I'm use this code for block the dollar button shift+4 = $.
On this table http://expandinghead.net/keycode.html the $ is code 36
now the code on keydown:
if (e.KeyValue == 36)
{
e.Handled = true;
}
code not work why?

Why not on KeyPress event
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '$')
{
e.Handled = true;
}
}

This is because you first press shift and then 4, so you will get the code of shift (key value 16) separately when using KeyDown event.
To achieve what you want, use KeyPress event, not KeyDown. KeyPress will register the character you typed ($), not individual keys pressed.
if (e.KeyChar == '$')
{
e.Handled = true;
}

Related

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

KeyPress Event gives wrong value

I made a KeyPress Event and want to allow only Double values (or just digits and comma) so I tried this:
e.Handled = !(char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Decimal);
But somehow he's got problems with the "Decimal". I'm using a german keyboard and when I try to enter the comma, he does nothing. When I press the "n" key he writes the letter. What is wrong here and how to solve that?
you can restrict the input using keyPress event like so:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char c= e.KeyChar;
if (!char.IsDigit(c) && !char.IsControl(c))
{
e.Handled = true;
}
}
if we want to extend our restriction condition to accept a certain character (for example ,)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char c= e.KeyChar;
if (!char.IsDigit(c) && !char.IsControl(c) && c!=',')
{
e.Handled = true;
}
}
To avoid having multiple comma like 222,34545,454 we can do this work around:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char c= e.KeyChar;
bool comma= textBox1.Text.Contains(','); //true in case comma already inserted
// accepts only digits, controls and comma
if (!char.IsDigit(c) && !char.IsControl(c) && c!=',')
{
e.Handled = true;
return;
}
// whenever a comma is inserted we check if we already have one
if (c == ',' && comma)
{
e.Handled = true;
}
}

how enter in TextBox only digits from interval

I try to make some form (C#, WinForms) for small program Reminder.
During it i have some problem - whant to make some filter for textBox where user must enter start time for event.
As result got next - on KeyPress event add some code, the allow enter only digits, but also i want that user can enter only digits from some diapasone.
code:
private void starTimetextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
{
return;
}
if (Char.IsControl(e.KeyChar))
{
if (e.KeyChar == (char) (Keys.Enter))
startTimeMMtextBox.Focus();
}
e.Handled = true;
}
can i do this by using this event or no?
Try this:-
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
NOTE:- You can use TextBox's MaxLength property to restrict user to enter only 2 digits. To display message to user you can use TextChanged event of TextBox.
To make a filter on the characters 0 to 9, you can extend the previous answer.
(Similair as in your question sample).
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)
&& (e.KeyChar >= '0' && e.KeyChar <= '9');
}

Trying to detect keypress

I made a method that detects when a key is pressed, but its not working! Heres my code
void KeyDetect(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W && firstload == true)
{
MessageBox.Show("Good, now move to that box over to your left");
firstload = false;
}
}
I also tried to make a keyeventhandler but, it sais "cannot assign to key detect because it is a method group"
public Gwindow()
{
this.KeyDetect += new KeyEventHandler(KeyDetect);
InitializeComponent();
}
Use keypress event like this:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.F1 && e.Alt)
{
//do something
}
}
1) Go to your form's Properties
2) Look for the "Misc" section and make sure "KeyPreview" is set to "True"
3) Go to your form's Events
4) Look for the "Key" section and double click "KeyDown" to generate a function to handle key down events
Here is some example code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("You pressed " + e.KeyCode);
if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
{
//Do Something if the 0 key is pressed (includes Num Pad 0)
}
}
You are looking for this.KeyPress. See How to Handle Keypress Events on MSDN.
Try to use the KeyDown event.
Just see KeyDown in MSDN
Just do
if (Input.GetKeyDown("/* KEYCODE HERE */"))
{
/* CODE HERE */
}

How to detect multiple keys entered in c# keydown event of textbox?

I want to design a numeric textbox in silverlight.
I have added keydown event of TextBox to handle the keypress.
Inside event I validate the key entered in the textbox.
event as follows
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
function Validate as follows
private bool Validate(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) //accept enter and tab
{
return true;
}
if (e.Key == Key.Tab)
{
return true;
}
if (e.Key < Key.D0 || e.Key > Key.D9) //accept number on alphnumeric key
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) //accept number fomr NumPad
if (e.Key != Key.Back) //accept backspace
return false;
return true;
}
I am not able to detect shift and Key.D0 to Key.D1 i.e.
SHIFT + 1 which returns "!"
SHIFT + 3 returns "#" like wise any other special keys.
I dont want user to enter special character in to textbox.
How do i handle this keys event??
In Silverlight, I don't think there are any Modifiers in the KeyEventArgs class but instead in Keyboard:
public void KeyDown(KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.D0)
{
ControlZero();
}
}
e should have a property on it that will tell you if Shift, Control or Alt are pressed. e.Modifiers can also give you additional information about which additional modifier keys have been pressed.
To cancel the character you can set e.Handled to True, which will cause the control to ignore the keypress.
textBox2.Text = string.Empty;
textBox2.Text = e.Modifiers.ToString() + " + " + e.KeyCode.ToString();

Categories