I have to detect decimal separator in current windows setting. Im using visual studio 2010, windows form. In particular, if DecimalSeparator is comma, if user input dot in textbox1, I need show zero in textbox2.
I tryed with this code, but not works:
private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
{
string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
if (uiSep.Equals(","))
{
while (e.KeyChar == (char)46)
{
tbxConvertito.Text = "0";
}
}
}
I have tryed also this code, but not work:
private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
{
string uiSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
if (uiSep.Equals(","))
{
if (e.KeyChar == (char)46)
{
tbxConvertito.Text = "0";
}
}
}
Solution:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (e.KeyChar == a)
{
e.Handled = true;
textBox1.Text = "0";
}
}
That way, when you hit . or , you will have a 0 in your TextBox.
EDIT:
If you want to insert a 0 everytime you hit the decimal separator, this is the code:
char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (e.KeyChar == a)
{
e.KeyChar = '0';
}
Actually you should be using
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
instead of
CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator
Using the second one gives you the OS default settings, which might be different then user Regional Locales for particular user account logged to this PC.
Credits to berhir and Grimm for pointing out the [docs]
You shouldn't use a while loop, I think it will freeze the application, use if instead, the problem might be here
Related
Amateur here making a C# calculator in visual studio and want to save inputs as 'operand1' and 'operand2' such that I can display the history of inputs/calculations. Example of my first thought:
private void twoBtn_Click(object sender, EventArgs e)
{
if (txtBox.Text == "0")
{
txtBox.Text = "2";
}
else
{
txtBox.Text += "2";
}
}
The issue is that it simply adds that to the text box, which is fine for calculations, but I have no way of retrieving that input data after it's cleared.
I've tried setting it up like this:
private void oneBtn_Click(object sender, EventArgs e)
{
if (operand1 == null)
{
operand1 = 1;
txtBox.Text = operand1.ToString();
return;
}
if (operand2 != null) return;
operand2 = 1;
txtBox.Text = operand2.ToString();
}
However, the issue here is that I cannot seem to input an operand which is more than 1 digit, making the only possible operands numbers 1-9. I understand I'm very new to the language and coding in general and would greatly appreciate anyone's insight as to what I'm missing!
I would just like to know how to create a textbox that only allows the user to type numbers, one that allows numbers and a fullstops and one that only allows the user to type letters?
I used this code for Windows Form:
private void YearText_KeyPress(object sender, KeyPressEventArgs e) //Textbox only accepts numbers
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 13)
{
e.Handled = true;
}
}
private void NameText_KeyPress(object sender, KeyPressEventArgs e) //Textbox only accepts letters
{
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))
e.Handled = true;
}
private void ResellPriceText_KeyPress(object sender, KeyPressEventArgs e) //Textbox that allows only numbers and fullstops
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
But I soon found out this can't be done with WPF. I'm not fussed about things such as the ability to paste letters/numbers.
This can be done in WPF, in fact you can even do it with very similar event handler based code, however, don't this - it is a terrible user experience. This will prevent users from copying and pasting when they accidentally include surrounding spaces and will prevent entering scientific numbers e.g. 100e3.
Instead use validations (on the trimmed input) and prevent the user from continuing if the validations fail.
I want to make a TextBox which does not allow to enter a value above 100. Only numbers allowed, And a Numeric TextBox is not an option. This is my code for now:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; } // only numbers
}
Any ideas?
You should use int.TryParse to see if the parsing is successful and then compare the value to see if it is below 100.
int number;
if(int.TryParse(textBox1.Text, out number))
{
if(number <= 100)
{
//in range
}
else
{
// not in range
}
}
else
{
//invalid number
}
You can also use double.TryParse or other TryParse method depending on the type, they are safe to use, since they will return a false if the parsing fails, instead of raising an exception.
Hello, here is my solution.
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
if ((!char.IsDigit(c) ||
Convert.ToInt32(textBox.Text + e.KeyChar) >= 101 ||
textBox.Text == "0") && c != '\b')
e.Handled = true;
}
Finally. I found a solution:
int box_int = 0; Int32.TryParse(textBox1.Text, out box_int);
if (box_int > 1050 && textBox1.Text != "") { textBox1.Text = "1050"; }
You can enter only numbers and use arrows keys and backspace. If you enter a number > than 100 or less than 1, when you press enter it will be cancelled. Copy and Past with button key down is disabled and also mouse right click to prevent the user to paste in the text box is disabled/handled. This should solve your problem in full.
First of all set:
ShortcutsEnabled property of your text box to False
this will not allow mouse right click and ctrl+V for paste in your text box.
Then add the following code:
//prevent letters, special chars, punctuation, symbols, white spaces
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
{
if (char.IsLetter(e.KeyChar) ||
char.IsSymbol(e.KeyChar) ||
char.IsWhiteSpace(e.KeyChar) ||
char.IsPunctuation(e.KeyChar))
e.Handled = true;
}
{
//allows only numbers between 1 and 100
string value = txtType1.Text;
if (txtType1.Text !="")
{
if (Int16.Parse(value) < 1 )
{
txtType1.Text = "";
}
else if (Int16.Parse(value) > 100)
{
txtType1.Text = "";
}
}
}
}
In a windows Form, I have a text box where I put amounts, for example I would type 18369.25 then press Enter key, the textbox should be formatted to: 18 369,25
how to do that ?
Subscribe to the textbox's KeyPress event with an event handler similar to the one below:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
decimal value;
if (decimal.TryParse(
textBox1.Text,
NumberStyles.Any,
CultureInfo.InvariantCulture,
out value))
{
textBox1.Text = value.ToString(
"### ### ##0.00",
CultureInfo.InvariantCulture).TrimStart().Replace(".", ",");
}
}
}
I did some expiriments, but none seemed to work. So I came out with this solution. I know its not the best one, but at least it work (for at least what you required):
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string s = textBox1.Text;
if (s.Contains('.'))
{
string[] arr = s.Split('.');
decimal dec = decimal.Parse(arr[0]);
textBox1.Text = string.Format("{0},{1}", dec.ToString("## ###"), arr[1]);
}
}
}
If you have any other requirements, please let me know.
bye
I m Working On A windows Form.. I Need my TextBox Not To Accept negative Values ..How Can I Do this..
IS There Any Property Availiable For Doing The same...
You need to write keypress event of textbox like :
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
You can also user numeric updown control to prevent negetive values.
UPDATE :
Ref: Sai Kalyan Akshinthala
My code will not handle the case of copy/paste. User can enter negative values by copy/paste. So I think Sai Kalyan Akshinthala's answer is correct for that case except one small change of Length >= 2.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
}
yes you can do write the following code part in textchanged event of textbox
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
just use min and pattern will not allow to enter a minus value
min="0" pattern="^[0-9]+$" in input type