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();
Related
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);
}
}
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;
}
How can I determine in KeyDown that ⇧ + Tab was pressed.
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift)
{
//do stuff
}
}
can't work, because never both keys are pressed exactly in the same second. You always to at first the Shift and then the other one..
It can't work, because never both keys are pressed exactly in the same second.
You're right that your code doesn't work, but your reason is wrong. The problem is that the Tab key has a special meaning - it causes the focus to change. Your event handler is not called.
If you use a different key instead of Tab, then your code will work fine.
If you really want to change the behaviour of Shift + Tab for one specific control, it can be done by overriding ProcessCmdKey but remember that many users use the Tab key to navigate around the form and changing the behaviour of this key may annoy those users.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (DateTimePicker_BirthDate.Focused && keyData == (Keys.Tab | Keys.Shift))
{
MessageBox.Show("shift + tab pressed");
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
If you are looking for a key press combination (Tab, then Shift) like Ctrl K + D you will have to use this modified example which was taken from MSDN social.
private StringBuilder _pressedKeys = new StringBuilder();
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
_pressedKeys.Append("Tab");
return;
}
if (e.Modifiers == Keys.Shift)
{
_pressedKeys.Append("Shift");
return;
}
if (_pressedKeys.ToString()."TabShift")
{
MessageBox.Show("It works!");
_pressedKeys.Clear();
}
else
{
_pressedKeys.Clear();
}
base.OnKeyDown(e);
}
First hook the Tab keypress event, then during the event, check the state of the Shift key. Keep in mind that there are two shift keys; make sure you check both of them.
This very related post shows how to check the state of modifier keys:
How to detect the currently pressed key?
Edit: an insight provided by another answerer who justly deserves an upvote is that the default behavior of the tab key (to change control focus) must be suppressed.
You can find your answer in
this post
It's Simple.
You can do that using KeyUp Event in the TextBox
private void txtBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
Or
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
It's Simple.
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
I use these following codes to work with numpad keys.
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
MessageBox.Show("You have pressed numpad0");
}
if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1)
{
MessageBox.Show("You have pressed numpad1");
}
And also for the other numpad keys. But I want to know how I can to this for "+" , "*" , "/" , " -" , " . " which located next to the numpad keys.
Thanks in advance
Check out the entire Keys enum . You have Keys.Mutiply, Keys.Add, and so forth.
Note that Keys.D0 is not the numpad 0, it's the non-numpad 0.
For "+" , "*" , "/" , we can use KeyDown event and for "-" , "." we can use KeyPress event.
Here are the codes :
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Add)
{
MessageBox.Show("You have Pressed '+'");
}
else if (e.KeyCode == Keys.Divide)
{
MessageBox.Show("You have Pressed '/'");
}
else if (e.KeyCode == Keys.Multiply)
{
MessageBox.Show("You have Pressed '*'");
}
}
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '.')
{
MessageBox.Show("You have pressed '.'");
}
else if (e.KeyChar == '-')
{
MessageBox.Show("You have pressed '-'");
}
}
I used switch to make mine work.
I am making a calculator and created a KeyDown even on the target textbox. I then used:
switch (e.KeyCode)
{
case Keys.NumPad1:
tbxDisplay.Text = tbxDisplay.Text + "1";
break;
case Keys.NumPad2:
tbxDisplay.Text = tbxDisplay.Text + "2";
break;
case Keys.NumPad3:
tbxDisplay.Text = tbxDisplay.Text + "3";
break;
}
etc.
The other thing to consider is that if the user then clicked an on screen button, the focus would be lost from the textbox and the key entries would no longer work. But thats easy fixed with a .focus() on the buttons.
There is a simple way to learn all the keycodes without checking the manuals.
Create a form and then go to KeyDown event of the form then add this
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}
Then you will get the name of the any key you have pressed on your keyboard
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)
{
}
}