0

Student working on ASP.NET project for VB that is trying to restrict users from entering anything accept numbers into the text boxes using a CASE statement, but I keep getting System.EventArgs errors.

My Code:

 Protected Sub ValueBox1_TextChanged(sender As Object, e As EventArgs) Handles ValueBox1.TextChanged
    Select Case e.KeyChar
    Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
    e.Handled = False
    Case Else
    e.Handled = True
    End Select
    End Sub

Errors = 'KeyChar' and 'Handled' are not members of 'System.EventArgs'

I have tried changing it to a KeyPress event

 (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)Handles TextBox1.KeyPress

but then the message changes to "KeyPress" cannot be found.I know I'm close but have tried so many other changes and suggestions it feels like I'm going in circles.

Trevor
  • 7,777
  • 6
  • 31
  • 50
the1fan
  • 131
  • 1
  • 10

2 Answers2

0

As you noticed, EventArgs type (base type for all other <Something>EventArgs) doesn't contain .KeyChar property (in fact, no properties at all).
Server side KeyPressed event is useless in Web applications.
So your options are check input on client side (in browser) with extra validation on server side.

  1. <asp:TextBox ID="txtNum" runat="server" TextMode="Number" min="0" max="20" />
    renders <input name="txtNum" type="number" id="txtNum" min="0" max="20">
    allows digits, +, -, .

  2. <asp:TextBox runat="server" TextMode="SingleLine" ID="txtNum2" /> <asp:RangeValidator runat="server" ID="valNum2" ControlToValidate="txtNum2" Display="Dynamic" ErrorMessage="Numbers only" MaximumValue="20" MinimumValue="0" Type="Integer"></asp:RangeValidator>
    Validates input after it changed and not allows to submit the form before the error fixed.

  3. Check every key press on client side using javascript.

    <asp:TextBox runat="server" ID="txtNum3" OnTextChanged="txtNum3_TextChanged" onkeydown="return checkKey(event)" />
    <script>
        function checkKey(evt) {
            if ('0123456789'.indexOf(evt.key) == -1)
                return false;
            return true;
        }
    </script>
    

Additionally check / validate input on server side.

Protected Sub txtNum3_TextChanged(sender As Object, e As EventArgs) Handles txtNum3.TextChanged  
      If Regex.IsMatch(txtNum3.Text, "\D") Then ''// \D - any non-digit character
        ''//do something to disapprove input
      End If


End Sub
Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36
  • Hi Alex, Thanks for the very helpful information. Just before I checked back here I kept searching ignoring my frustrations and I found something similar to what you mentioned, by changing the ASP page versus the VB code. I updated my ASP Page with That gave me the desired effect so that I could move forward and complete my project. – the1fan Sep 23 '18 at 21:34
  • Just make sure that `.keyCode` is deprecated https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode `event.key` is recommended. – Alex Kudryashev Sep 24 '18 at 00:02
  • Dear downvoter, please explain what is wrong. – Alex Kudryashev Sep 24 '18 at 00:03
  • @the1fan If you wish inline script then `onkeydown="return '0123456789'.indexOf(event.key)>-1;"` works better. – Alex Kudryashev Sep 24 '18 at 00:16
  • @AlexKudryashev how to allow canc and backspace too? – Gabriele Cozzolino Oct 07 '22 at 07:25
-1

In the classic mode of "Here is one I prepared earlier" ...

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If Not (e.KeyChar >= "0" And e.KeyChar <= "9") Then
        e.KeyChar = Chr(0)
    End If
End Sub

In your post, you swapped ValueBox1 for TextBox1. And KeyChar is not valid for the generic EventArgs, but is for the specific KeyPressEventArgs.

If that does not work in your context, then check your solution references because this code has worked for me in the past.

AJD
  • 2,400
  • 2
  • 12
  • 22
  • My bad, it should be "ValueBox1" instead of "TextBox1". When I used what you have I get two errors: – the1fan Sep 23 '18 at 20:15
  • Error 1 Event 'KeyPress' cannot be found. Error 2 Expression is a value and therefore cannot be the target of an assignment. – the1fan Sep 23 '18 at 20:16
  • `If Not (IsNumeric(e.KeyChar)) Then e.Handled = True End If`. – Jimi Sep 23 '18 at 21:10