0

Hello everyone i made C# program and i want to make textbox only accept Number i tryed many different code and i get same errors please help this is my code

    private void agetxt_Click(object sender, EventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }
    }

and this is the error message i get

Error CS1061 'EventArgs' does not contain a definition for 'KeyChar' and no extension method 'KeyChar' accepting a first argument of type 'EventArgs' could be found (are you missing a using directive or an assembly reference?) WindowsFormsApplication1 C:\Users\ziadm\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form2.cs 176 Active

Ziad Saad
  • 43
  • 2
  • 6
  • 1
    variable e is of type EventArgs, the class EventArgs does not have a public variable KeyChar. did you want to call a function like getKeyChar() ? or maybe the variable KeyChar in EventArgs is not public. – Donat Pants Apr 08 '17 at 20:16
  • Possible duplicate of [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) – Andrew Morton Apr 10 '17 at 20:21

1 Answers1

4

You're using a Click event which doesn't contain the KeyChar property. You'd probably like to use the KeyPress event on the TextBox and implement the same logic which incorporates the KeyChar property to be able to check if the typed character is a digit or not.

 public Form1()
    {
        InitializeComponent();
        agetxt.KeyPress += agetxt_KeyPress;
    }


    private void agetxt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }
    }
Westerlund.io
  • 2,743
  • 5
  • 30
  • 37
  • are you sure this is going to work, why should this not cause the same error? – Donat Pants Apr 08 '17 at 20:20
  • When you use the `KeyPress` event it uses KeyPressEventArgs which contains the `KeyChar` property. `EventArgs` doesn't contain the `KeyChar` property. I tried my code out and it only allows numbers. – Westerlund.io Apr 08 '17 at 20:23
  • i didn't notice you changes EventArgs e to KeyPressEventArgs e, my bad, this will work. – Donat Pants Apr 08 '17 at 20:25
  • Note that OP uses a `Click` event and I use the `KeyPress` event. – Westerlund.io Apr 08 '17 at 20:25
  • i changed it to the Keypress From the event Thanks again @DonatPants – Ziad Saad Apr 08 '17 at 20:27
  • @AndrewMorton Updated answer! – Westerlund.io Apr 08 '17 at 20:29
  • Don't suggest keypress events for input validation. Users can still paste text containing non-numeric characters using the context menu, but not navigate through text anymore using arrow keys, Delete or Backspace using this approach. This answer maybe fixes the compiler error, but the underlying problem of the OP is not solved by this. – CodeCaster Apr 08 '17 at 20:30
  • @CodeCaster For all we know, the OP posted an MVCE and has other code to handle paste. Otherwise, they're going to go through a learning process. – Andrew Morton Apr 08 '17 at 20:39
  • @Andrew the approach is broken by design. Like I said, you can fix the compiler errors, but then it's still broken. Again: this code prevents the use of Backspace, but still allows non-numerical characters to be pasted into the textbox. It is not a good answer. – CodeCaster Apr 08 '17 at 20:40