CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question Allowing only number in the textbox

    i want text box only needs to allowed numeric no .in other way round . i need to adapt the following code in vb.net .let me know please .
    Code:
    Private Sub TxtWeekNo_KeyPress(KeyAscii As Integer)
    If KeyAscii = 8 Or KeyAscii = 27 Or KeyAscii = 13 Then Exit Sub 'we need to allow backspace & some other key
    If Not (KeyAscii > 47 And KeyAscii < 58) Then
    KeyAscii = 0
    End If
    End Sub

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Allowing only number in the textbox

    This is probably the best option out there :

    http://www.vbforums.com/showthread.php?t=618778

  3. #3
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Allowing only number in the textbox

    Not sure how KeyAscii gets to this Sub. Looks like it is a number already. If you are asking about how to test a value in a text box you can use the IsNumeric function.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Allowing only number in the textbox

    Quote Originally Posted by Mur16 View Post
    Not sure how KeyAscii gets to this Sub. Looks like it is a number already. If you are asking about how to test a value in a text box you can use the IsNumeric function.
    That was VB 6 code, that the OP wants to 'convert' into .NET

  5. #5
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Allowing only number in the textbox

    I see . . .
    Code:
    Private Sub TxtWeekNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtWeekNo.TextChanged
    ' test to see if TxtWeekNo.Text IsNumeric
    End Sub

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Allowing only number in the textbox

    I agree with your test for IsNumeric. The problem I can foresee here is the fact that sometimes special keys also needs allowance. Keys such as the Backspace, perhaps even the Delete key should be allowed. Another thing to consider here is the formatting of the numbers, ie, the presenting of them. Will decimals be allowed. Will a thousands-separator be needed and so forth. I was initially thinking about the Masked Edit Box, but decided to go with the VBForums link, as it is more useful at the end of the day

  7. #7
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: Allowing only number in the textbox


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured