开发者

How to accept only one decimal point in a textbox?

The textbox will开发者_开发知识库 only accept numbers and only one decimal point.

For example the textbox contains "12345.56". When the period is pressed a second time on the keyboard it must not appear in the textbox because the textbox already contains a period.


[0-9]+(\.[0-9][0-9]?)?

Go with regular expressions.


hande KeyPress event and assuming its windows

void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == '.' )
        {
            if (richTextBox1.Text.Contains('.'))
                e.Handled = true;
        }
    }


**//Only numeric with Two decimal place comes in textbox and if user want to enter decimal again or space it will not allowed.**

 if ((e.Key >= Key.D0 && e.Key <= Key.D9 ||
                e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || e.Key == Key.Decimal || e.Key == Key.OemPeriod))
            {
                string strkey = e.Key.ToString().Substring(e.Key.ToString().Length - 1, e.Key.ToString().Length - (e.Key.ToString().Length - 1));

                if (e.Key >= Key.D0 && e.Key <= Key.D9 ||
                    e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                {

                    TextBox tb = sender as TextBox;
                    int cursorPosLeft = tb.SelectionStart;
                    int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                    string result1 = tb.Text.Substring(0, cursorPosLeft) + strkey + tb.Text.Substring(cursorPosRight);
                    string[] parts = result1.Split('.');
                    if (parts.Length > 1)
                    {
                        if (parts[1].Length > 2 || parts.Length > 2)
                        {
                            e.Handled = true;
                        }
                    }
                }

                if (((TextBox)sender).Text.Contains(".") && e.Key == Key.Decimal)
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }

            if (e.Key >= Key.A && e.Key <= Key.Z ||
                    e.Key == Key.Space)
            {
                e.Handled = true;
            }

            if (Keyboard.Modifiers == ModifierKeys.Shift && e.Key == Key.OemPeriod)
            {
                e.Handled = true;
            }


private bool isValueEnteredExceed100(string textBoxValue, string inputText)
{
    var countPeriod = textBoxValue.Count(x => x.ToString().Equals("."));
    if (countPeriod <= 1)
    {
        bool isValidNumber = AreAllValidNumericChars(inputText);

        if (isValidNumber == true || inputText == ".")
        {
            double enterdValue;
            bool returnValue = false;
            if (textBoxValue != string.Empty || textBoxValue != "")
            {
                enterdValue = Convert.ToDouble(textBoxValue);
                if (enterdValue > 0 && enterdValue <= 100)
                {
                    returnValue = true;
                }
            }
            return returnValue;
        }
        else
        {
            return isValidNumber;
        }
    }
    else
    {
        return false;
    }
}

private void AcceptedFromTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    string textBoxValue = AcceptedFromTextBox.Text + e.Text;
    if (textBoxValue == ".")
    {
        textBoxValue = "0" + e.Text;
        AcceptedFromTextBox.Text = textBoxValue;
    }
    e.Handled = !isValueEnteredExceed100(textBoxValue, e.Text);
    AcceptedFromTextBox.SelectionStart = AcceptedFromTextBox.Text.Length;
}


private bool AreAllValidNumericChars(string str)
{
    Regex regex = new Regex(@"[0-9]$");
    return regex.IsMatch(str);
}


Another example ,

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{

    if (txtPrice.Text.Length == 0)
    {
        if (e.KeyChar == '.')
        {
            e.Handled = true;
        }
    }
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
    {
        e.Handled = true;
    }
    if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}


Code on Keypress event of textbox

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
       e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
       e.Handled = true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜