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;
}
}
Related
I want to write temperature in my textbox for example:"38.8". But I dont want to use maskedTextbox.
My code:
private void textBox_TextChanged()
{
if(textbox. text. length() == 3)
{
textbox. Text += "." ;
}
}
But it doesnt work. I would like that the backspace key avoid comma. How to do that?
Not the best way but will do the job. You can use KeyPress event for your textbox
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
if ((e.KeyChar != (char)System.Windows.Forms.Keys.Back))
{
if ((sender as TextBox).Text.Length == 2)//set dot(.) after 2 numbers.
{
(sender as TextBox).Text += ".";
(sender as TextBox).SelectionStart = this.Text.Length;
}
}
}
I have used e.Handled bool of KeyPress event and let e.KeyChar only be D0-D9, ',' and Back. But then my program started inputting an ASCII character into the TextBox instead of erasing the most recent char when I pressed Backspace. Then I assigned;
if(e.KeyChar == (char)Keys.Back) {textBox1.Text = textBox1.Text + "\b"}
to erase the most recent char that was inputted. It still decided to input a weird ASCII character instead of erasing anything.
If you want to allow characters in '0'..'9' range only, while keeping all the other logic intact (for instance, if you select several characters and then press "BackSpace" only selection will be removed, not the last character) you can handle unwanted characters only:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
e.Handled = e.KeyChar >= ' ' && (e.KeyChar < '0' || e.KeyChar > '9');
}
Note, that e.KeyChar >= ' ' allowes all command characters (BS included)
this worked:
if (e.KeyChar == (char)Keys.Back)
{
textBox1.Text = textBox1.Text.Substring(0, (textBox1.Text.Length - 1));
}
Please try with the following code.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)Keys.Back &&
(e.KeyChar < (char)Keys.D0 || e.KeyChar > (char)Keys.D9))
{
e.Handled = true;
}
}
I try constraint for TextBox in C#. I succesfull this for users can put only numbers:
private void TxtID_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
Now, I wonder how can users just write 11 char in TextBox? I try char.Max or Min but i can't in the KeyPress event.
Something like this:
public MyForm()
{
InitializeComponent();
// At most 11 characters
TxtID.MaxLength = 11;
}
private void TxtID_KeyPress(object sender, KeyPressEventArgs e)
{
// char.IsDigit is too wide: it returns true on any unicode digit (e.g. Persian ones)
e.Handled = (e.KeyChar < '0' || e.KeyChar > '9') && !char.IsControl(e.KeyChar);
}
// On Paste we should validate the input:
// what if user copy "bla-bla-bla 1234" and paste it to TxtID?
private void TxtID_TextChanged(object sender, EventArgs e)
{
Control ctrl = (sender as Control);
string value = string.Concat(ctrl
.Text
.Where(c => c >= '0' && c <= '9'));
if (value != ctrl.Text)
ctrl.Text = value;
}
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');
}
I use below code to not allowing any character except numbers in a textbox ... but it allows '.' character! I don't want it to allow dot.
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)(Keys.Back)&&
e.KeyChar !=(char)(Keys.OemPeriod))
{
e.Handled = true;
}
}
use this:
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)
)
it is because Keys.Delete's char value is 46 which is the same as '.'. I do not know why it likes this.
You could try this instead (where textBox1 would be your textbox):
// Hook up the text changed event.
textBox1.TextChanged += textBox1_TextChanged;
...
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Replace all non-digit char's with empty string.
textBox1.Text = Regex.Replace(textBox1.Text, #"[^\d]", "");
}
Or
// Save the regular expression object globally (so it won't be created every time the text is changed).
Regex reg = new Regex(#"[^\d]");
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (reg.IsMatch(textBox1.Text))
textBox1.Text = reg.Replace(textBox1.Text, ""); // Replace only if it matches.
}
//This is the shortest way
private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
try this code for your problem in keypress event :
private void txtMazaneh_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && (int)e.KeyChar != 8 ||(e.KeyChar= .))
e.Handled = true;
}