How to get pressed character from KeyDown event windows phone - silverlight

I am developing a windows phone app. I want to get the value of the key in which the user is pressing. I want check wheather it is a digit or any other alphabet or special charater. Because my text box is using for entering currency. So I need to prevent users from entering alphabets or any special characters. Only digits are allowed to enter.

This is how I usually reject keystrokes
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.D1)
{
// reject this key and do not show in textbox
e.Handled = true;
}
}
Edit: See How to create a numeric textbox in Silverlight? for a more through implementation.

keydown event handler:
KeyEventArgs e
e.Key is enum type Key, you can get character enum from this paramter

To restrict the user in entering digits only, set the inputscope to Number. See this for more details: http://msdn.microsoft.com/en-us/library/windowsphone/develop/gg521152(v=vs.92).aspx

Related

Chinese password in masked text box in Windows Forms?

When a Windows Forms TextBox is in password mode, it is restricted and the ImeMode is disabled (As discussed here and here). How can I design a textbox that doesn't reveal the user's input but that takes/collects the Chinese input from the keyboard (basically a workaround for taking in Chinese input in a password TextBox)?
You can use the OnKeyPress event to intercept input before it appears in the TextBox. Save the input somewhere else and put some masking char into the TextBox.
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//save the key pressed
TextBox1.Text += "*";
e.handled = true;
}

How can I get more than one character to show in a messagebox from a textbox user input?

I have made a textbox and I want the user to type in a string of numbers and hit enter. I have setup the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
String UserBarcode;
Focus();
UserBarcode = Console.ReadLine();
MessageBox.Show(UserBarcode);
}
When I enter any key into the textbox, I get a message box with nothing in it. I want to have the program wait til it hears the enter key then display the contents of the textbox.
The Textbox.TextChanged event fires as soon as the text in the textbox is changed at all. If you want a message box with the full string, you probably want to consider using the Textbox.LostFocus event or a button's Click event.
So you could have something like (I'm taking a stab at this here, as I've used VB rather than C#)
private void textBox1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show(sender.Text)
}
If you're using a button, the above function should work, but you'll want to substitute textBox1.Text for sender.Text.
Take a look at Focus and Validation Events
There are several events that you can handle, depending on your goals and how your application is designed. If you want to perform validation and/or are using data binding, you may want to go with handling the validating/validated events. By default data bindings update a bound property after OnValidating. If you use LostFocus and read the value from a bound object, instead of your control, you will get inconsistent results.
I was able to figure it out finally. For some reason when I manually entered the code I kept getting multiple random errors. I started a new Visual C # Windows Forms Application, Made a textbox, chose the keydown property and double clicked on it to have the program inject the code for the keydown function and then I filled in the if statement pointing to the enter key. The final code looks like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}

How to validate textboxes in C#.net winforms

I have a form where I need to validate the textboxes like Firstname, Middlename, Lastname, emailId, date, mobilenumber. The validation should happen when a user starts typing in the textbox. errorprovider should show message under textbox if a user enters numbers in place of text and text in place of numbers. I got to know about implicit validation and explicit validation but I feel better to use implicit validation only because its on time error provider when user looses focus on text box or if he shifts to another textbox. I've posed this kind of question with a explicit validation code but no one responded me. So Im making it simple to get help. Do not think I havent done enough research before posting this question.
If you have a very specific validation to do, Marc's answer is correct. However, if you only ensure the "enter number instead of letters" or "enter letters instead of numbers" thing, a MaskedTextBox would do the job better than you (user wouldn't be able to answer incorrect data, and you can still warn them by handling the MaskInputRejected event)
http://msdn.microsoft.com/en-us/library/kkx4h3az(v=vs.100).aspx
You should take a look to the TextChanged Event in of your textbox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx
This event is raised if the Text property is changed by either a
programmatic modification or user interaction.
I would do something like this
private void TextBoxExample_TextChanged(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
if (box.Text.Contains("Example"))
{
LabelError.Text = "Error";
}
else
{
LabelError.Text = string.Empty;
}
}
Hope it helps :)
You can also use keyPressEvent to Avoid the entering the numerical values in the textboxes
it will not allow the numerical chars in the text box
private void textboxName_KeyPress(object sender, KeyPressEventArgs e)
{
//not allowing the non character values
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !(e.KeyChar == (char)Keys.Back) && !(e.KeyChar == (char)Keys.Left) && !(e.KeyChar == (char)Keys.Right) && !(e.KeyChar == (char)Keys.Space) && !char.IsPunctuation(e.KeyChar))
{
e.Handled = true;
}
}

Detect “tab” key press from the last cell/row of a DataGrid

In a WPF Datagrid, how to detect when the user press the key "Tab" from the last cell/row ? With KEY_DOWN event the selected cell is unknown, with FOCUS_OUT we don't know the key pressed.
private void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
MessageBox.Show("now tab!!!");//Here u know the tab press
MessageBox.Show(dataGrid1.SelectedIndex.ToString());//Here u know the cell/row!
}
}
You want to handle the PreviewKeyDown event on the DataGrid itself. Within the handler you can then check to see which cell is currently selected.

Can I determine which keyboard a key pressed is on?

Users of my application have a second keyboard with special function keys. Unfortunately, the keys are mapped to buttons such as F, G, F1 and so on. I would like to handle PreviewKeyDown and prevent any keys from these keyboards having an effect in normal controls such as TextBoxes.
In WPF, is there any way of determining which keyboard raised the event?
No, it is not possible directly in WPF.
using System.Windows.Input you could be able to achieve this by capturing the event that is fired in your code behind. Sample code below shows how this can be done in Textbox.
private void SampleTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete) // delete key is pressed
{
e.Handled = true; // Ignore key press
}
}

Resources