Pergunta

Please excuse me for asking this, I have looked at various different questions relating to this and I still can not get it to implement.

Using the answers I have looked at, I have gathered this, and applied this coding to my text box.

private void TxtBox5_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
            e.Handled = true;
    }

Now, when I proceed to run the program, I am still able to enter letters into it. I do not know what to do next, so any solution would be great. thanks.

Foi útil?

Solução

Okay this is what i made just now, it works 100% just tested it.Note that my textBox is named serialTxtBox, you can change it to yours.

void serialTxtBox_TextChanged(object sender, EventArgs e)
        {
            bool enteredLetter = false;
            Queue<char> text = new Queue<char>();
            foreach (var ch in this.serialTxtBox.Text)
            {
                if (char.IsDigit(ch))
                {
                    text.Enqueue(ch);
                }
                else
                {
                    enteredLetter = true;
                }
            }

            if (enteredLetter)
            {
                StringBuilder sb = new StringBuilder();
                while (text.Count > 0)
                {
                    sb.Append(text.Dequeue());
                }                

                this.serialTxtBox.Text = sb.ToString();
                this.serialTxtBox.SelectionStart = this.serialTxtBox.Text.Length;
            }
        }

EDIT: Definitely you are doing something wrong. In your form constructor which is named like your form. In my case SerialGenerator, you need to initialize the event. In my case :

public SerialGenerator()
        {
            InitializeComponent();
            this.serialTxtBox.TextChanged += serialTxtBox_TextChanged;
        }

this will fire the method everytime someone enters something in your textBox. Make sure you rename it to your textbox's name

Outras dicas

You can try this:

private void TxtBox5_KeyPress(object sender, KeyPressEventArgs e)
{
    if(!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)))
        { e.Handled = true; }
}

I don't think this is the best one, but you may make this work by tweaking a little, I guess. I don't have VS right now to check what would work.

EDIT: My bad. I think you can use the above one not on keypress but on textchanged event of the textbox. It's more like a tweak than a solution. Just to make you progress if you are stuck and don't get a better solution.

Update: Updated the code. Please check if this one helps you.

Try this one,hope this works

   private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        int asciiCode = Convert.ToInt32(e.KeyChar);
        if ((asciiCode >= 48 && asciiCode <= 57))
        {


        }
        else
       {                MessageBox.Show("Not allowed!");
            e.Handled = true;


        }
    }

I have added events on code level, since there can be a possibility in your code not correctly event added to the control.

Note : The KeyPress event is not raised by noncharacter keys you need to use both KeyDown and KeyPress events

public partial class Form1 : Form
{
    private bool nonNumberEntered = false;
    public Form1()
    {
        InitializeComponent();
        textBox1.KeyDown+=new KeyEventHandler(textBox1_KeyDown);
        textBox1.KeyPress+=new KeyPressEventHandler(textBox1_KeyPress);
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (nonNumberEntered == true)
        {
            e.Handled = true;
        }
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        nonNumberEntered = false;

        if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
                if (e.KeyCode != Keys.Back)
                {
                    nonNumberEntered = true;
                }
            }
        }
        if (Control.ModifierKeys == Keys.Shift)
        {
            nonNumberEntered = true;
        }
    }
}

REF : Control.KeyPress Event

Try this ...

private void TxtBox5_KeyPress(object sender, KeyPressEventArgs e)
    {         
        int num = 0;
         e.Handled = !int.TryParse(e.KeyChar.ToString(), out num);

    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top