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');
}
Related
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 need to check that if my TextBox (txtmoney) is empty or equal to 0, it's not allowed to press key 0 on keyboard or the number 0.
How can I do this?
Before implementing please refer TextChanged & KeyPress
If you dont want to allow the 0 to be entered then
private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == '0');
}
To handle textbox text you can try is
private void txtBox_TextChanged(object sender, KeyPressEventArgs e)
{
if (String.IsNullOrEmpty(textbox.Text) || txtmoney.Text == "0")
{
// Do Something
}
}
Note: if you are using winform don't forget to subscribe those event in your code.
Example:-
txtBox.KeyPress += txtBox_KeyPress;
txtBox.TextChanged += txtBox_TextChanged;
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;
}
}
Just want to ask how to set text to all caps when typing in textbox
I've tried this, but it's not working.
void txt_AllCaps(object sender, KeyPressEventArgs e)
{
string s = (sender as TextBox).Text.ToString().ToUpper();
(sender as TextBox).Text = s;
}
try:
YourTextBox.CharacterCasing = CharacterCasing.Upper;
You can change the e.KeyChar in the KeyPress event handler to what you want. Try this:
private void txt_AllCaps(object sender, KeyPressEventArgs e){
e.KeyChar = e.KeyChar.ToString().ToUpper()[0];
//Or this
//if (e.KeyChar > 96 && e.KeyChar < 123) e.KeyChar = (char) (e.KeyChar - 32);
}
You should choose the solution of Shree, it's much more convenient :)
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;
}