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;
}
}
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;
}
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 can I make a TextBox only accept alphabetic characters with spaces?
You could use the following snippet:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z ]"))
{
MessageBox.Show("This textbox accepts only alphabetical characters");
textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
You can try by handling the KeyPress event for the textbox
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
}
Additionally say allow backspace in case you want to remove some text, this should work perfectly fine for you
EDIT
The above code won't work for paste in the field for which i believe you will have to use TextChanged event but then it would be a bit more complicated with you having to remove the incorrect char or highlight it and place the cursor for the user to make the correction Or maybe you could validate once the user has entered the complete text and tabs off the control.
private void textbox1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key >= Key.A && e.Key <= Key.Z)
{
}
else
{
e.Handled = true;
}
}
The simplest way is to handle the TextChangedEvent and check what's been typed:
string oldText = string.Empty;
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text.All(chr => char.IsLetter(chr)))
{
oldText = textBox2.Text;
textBox2.Text = oldText;
textBox2.BackColor = System.Drawing.Color.White;
textBox2.ForeColor = System.Drawing.Color.Black;
}
else
{
textBox2.Text = oldText;
textBox2.BackColor = System.Drawing.Color.Red;
textBox2.ForeColor = System.Drawing.Color.White;
}
textBox2.SelectionStart = textBox2.Text.Length;
}
This is a regex-free version if you prefer. It will make the text box blink on bad input.
Please note that it also seems to support paste operations as well.
Write Code in Text_KeyPress Event as
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsLetter(e.KeyChar))
{
e.Handled = true;
}
}
This one is working absolutely fine...
private void manufacturerOrSupplierTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsControl(e.KeyChar) || char.IsLetter(e.KeyChar))
{
return;
}
e.Handled = true;
}
This solution uses regular expressions, does not allow invalid characters to be pasted into the text box and maintains the cursor position.
using System.Text.RegularExpressions;
int CursorWas;
string WhatItWas;
private void textBox1_Enter(object sender, EventArgs e)
{
WhatItWas = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (Regex.IsMatch(textBox1.Text, "^[a-zA-Z ]*$"))
{
WhatItWas = textBox1.Text;
}
else
{
CursorWas = textBox1.SelectionStart == 0 ? 0 : textBox1.SelectionStart - 1;
textBox1.Text = WhatItWas;
textBox1.SelectionStart = CursorWas;
}
}
Note: textBox1_TextChanged recursive call.
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]+$"))
{
}
else
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
MessageBox.Show("Enter only Alphabets");
}
Please Try this
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= '0' && e.KeyChar <= '9')
e.Handled = true;
else
e.Handled = false;
}
Try This
private void tbCustomerName_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back||e.KeyChar==(char)Keys.Space);
}
It Allows White Spaces Too
you can try following code that alert at the time of key press event
private void tbOwnerName_KeyPress(object sender, KeyPressEventArgs e)
{
//===================to accept only charactrs & space/backspace=============================================
if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space))
{
e.Handled = true;
base.OnKeyPress(e);
MessageBox.Show("enter characters only");
}
Here is my solution and it works as planned:
string errmsg = "ERROR : Wrong input";
ErrorLbl.Text = errmsg;
if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space))
{
ErrorLbl.Text = "ERROR : Wrong input";
}
else ErrorLbl.Text = string.Empty;
if (ErrorLbl.Text == errmsg)
{
Nametxt.Text = string.Empty;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) &&
(e.KeyChar !='.'))
{
e.Handled = true;
MessageBox.Show("Only Alphabets");
}
}
Try following code in KeyPress event of textbox
if (char.IsLetter(e.KeyChar) == false &
Convert.ToString(e.KeyChar) != Microsoft.VisualBasic.Constants.vbBack)
e.Handled = true
works for me, even though not the simplest one.
private void Alpha_Click(object sender, EventArgs e)
{
int count = 0;
foreach (char letter in inputTXT.Text)
{
if (Char.IsLetter(letter))
{
count++;
}
else
{
count = 0;
}
}
if (count != inputTXT.Text.Length)
{
errorBox.Text = "The input text must contain only alphabetic characters";
}
else
{
errorBox.Text = "";
}
}
This works fine as far as characters restriction, Any suggestions on error msg prompt with my code if it's not C OR L
Private Sub TXTBOX_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTBOX.TextChanged
Dim allowed As String = "C,L"
For Each C As Char In TXTBOX.Text
If allowed.Contains(C) = False Then
TXTBOX.Text = TXTBOX.Text.Remove(TXTBOX.SelectionStart - 1, 1)
TXTBOX.Select(TXTBOX.Text.Count, 0)
End If
Next
End Sub
Try this one. Spaces and shortcut keys work
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsSeparator(e.KeyChar))
{
e.Handled = true;
}