Check TextBox - C #

I have a pretty hard time checking my text box in a C # application. The fact is that the specified text field should take only decimal values. therefore, this means that there should be no letters or any other characters besides ".". symbol. A letter filter, I can handle it. However, I don’t know exactly how I will be able to filter out the quantity ".". what the text box should accept. If anyone knows how to do this, please give me an idea.

Many thanks:)

+3
source share
5 answers

it should work !!!

modified for only one decimal place

    private void txtType_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Back || (e.KeyChar == (char)'.') && !(sender as TextBox).Text.Contains("."))
        {
            return;
        }
        decimal isNumber = 0;
        e.Handled = !decimal.TryParse(e.KeyChar.ToString(), out isNumber);
    }
+3
decimal value;
bool isValid = decimal.TryParse(textBox.Text, out value);

if (!isValid)
{
    throw new ArgumentException("Input must be a decimal value");
}
+4

: , bool , " "; .

, Contains:

if (textbox.Text.Contains("."))

, MSDN (NumericTextBox):

http://msdn.microsoft.com/en-us/library/ms229644(VS.80).aspx

+1
+1

MaskedTextBox .

0

Source: https://habr.com/ru/post/1754634/


All Articles