0

I want to disable special character from text box in winforms (specially + and - symbol). I do have idea of e.Handled, but I can't use it as I am using some components which only exposes KeyAscii property to me.

public sealed class KeyPressEventArgs
        : System.EventArgs 
        {

            public int KeyAscii = 0;
            public KeyPressEventArgs(int KeyAscii): base()
            {
                this.KeyAscii = KeyAscii;
            }
    }
DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
Pankaj
  • 1,446
  • 4
  • 19
  • 31
  • 1
    There is no such thing as a "C# textbox", only a WPF TextBox, Windows Forms TextBox, ASP.NET TextBox etc. – Sebastian Negraszus Aug 05 '15 at 10:38
  • Probable duplicate of : http://stackoverflow.com/questions/19524105/how-to-block-or-restrict-special-characters-from-textbox – DeshDeep Singh Aug 05 '15 at 10:43
  • I know there are lot of questions like this.... but i have already mentioned in the question that I can't use e.Handled .... so I won't say its duplicate – Pankaj Aug 05 '15 at 10:45
  • @DeshDeepSingh the KeyPress event and the Handled property are _not_ for input validation. Answers that advise such are wrong and broken. It is for example easily bypassed by pasting. – CodeCaster Aug 05 '15 at 10:48
  • OP what kind of TextBox is this? Clearly not WinForms, as you say you're using "some component" that exposes a different KeyPress event. – CodeCaster Aug 05 '15 at 10:48
  • You can use `TextChanged` event and `Regex.Replace()` to filter values. This should work for you – Fabjan Aug 05 '15 at 11:54

1 Answers1

0

You may take a look at Validators. https://msdn.microsoft.com/en-us/library/ms229603%28v=vs.110%29.aspx

Or you create a custom control (textbox). http://www.codeproject.com/Articles/2016/Writing-your-Custom-Control-step-by-step

Chaka
  • 377
  • 1
  • 9