1

i'm using regex to keep input into a text box numbers only and trying to make any non number inserted removed

private void txtNex_TextChanged(object sender, EventArgs e)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(txtNex.Text, "[^0-9]"))
    {
        MessageBox.Show("insert numbers only");
        if (txtNex.Text.Length > 1)
        {
            txtNex.Text = txtNex.Text.Substring(0, txtNex.Text.Length - 1);
        }
        else
        {
            txtNexFixPhone.Text = "0";
        }
    }
}

the problem is while it does work there is some sort of a bug (or my own lack of knowledge) that moves the input to the beginning and if i enter another non numerical it will make a loop that removes all text

so lets say i enter

123a

it will give me and error messagebox and remove the "a" now if i try to input another "a" it will come before the 123

a123

ending with an error loop that will delete all input

dario
  • 5,149
  • 12
  • 28
  • 32
Iakovl
  • 1,013
  • 2
  • 13
  • 20

3 Answers3

1

For one this approach is a bad idea because you check the input on TextChanged which is already too late - the invalid character is there and you now have to remove it - it would be better to do it on KeyPress and prevent the input altogether so that you don't have to manipulate the text at all:

Example:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!Char.IsDigit(e.KeyChar))
    {
        MessageBox.Show("insert numbers only");
        e.Handled = true;
    }
}

If you however want to fix the bug that moves the input to the beginning and keep your current solution you need to put the cursor at the end after you manipulated the text because setting a new text to the textbox resets the cursor's position.

Example:

// Manipulate the text:
txtNex.Text = txtNex.Text.Substring(0, txtNex.Text.Length - 1);

// Put the cursor at the end:
txtNex.Select(txtNex.Text.Length, 0);
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • 1
    This is the way to go if you don't want non-numeric input. Correcting input after-the-fact has far too many use cases that need handling. Handling the KeyPress event ignores the non-numeric key the moment it happens. For instance, even if you manipulate the text and place the cursor at the end, the user has the option to hit the left arrow, or click elsewhere in the input - again resulting in your loss of input issue. But handling the KeyPress will be unhindered by cursor location. I.e. "Better to ask permission here, rather than forgiveness." – OhBeWise Jan 16 '15 at 23:47
0

It is better to validate the value of the checkbox instead of don't allow enter something. You can do something like this

decimal number = 0;

if(!decimal.TryParse(txtNex.Text, number))
{
    //Error message
}

If you enter the if block, the text in the textbox can not be parsed to decimal value . You can make custom validation method which you will call on every place when you need to use textbox value.

EDIT:

Looks like you don't like my answer. Here is the code which will clear not numbers

   private void txtNex_TextChanged(object sender, EventArgs e)
   {
        string text = txtNex.Text;
        string loopText = txtNex.Text;

        foreach (Char c in loopText)
        {
            string s1 = c.ToString();
            if(Regex.IsMatch(s1, "[^0-9]"))
            {
                if (text.Contains(s1))
                    text = text.Replace(s1, "");
            }
        }

        txtNex.Text = text;
   }
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • i can always use a check when i want to submit, my point was making a fix and an error message while typing – Iakovl Jan 16 '15 at 19:38
  • 2
    First your logic is not user friendly and also you assume that only last character is not a number, which will not lead to anything good. What I wrote is best and simple way of doing it. – mybirthname Jan 16 '15 at 19:44
  • I think you wanted to parse `TextBox`'es `.Text` and not the `.Length` in you first example :-) – t3chb0t Jan 17 '15 at 08:52
0

This will probably work:

MessageBox.Show("insert numbers only");
if (txtNex.Text.Length > 1)
{
     Regex rgx = new Regex("([^0-9]*)");
     txtNex.Text = rgx.Replace(txtNex.Text, "");
}