CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2001
    Posts
    67

    Validation of entries in textbox

    I wish to be able to enter only positive or negative numbers into a textbox.How can I go about doing this? Is there any inherent vbmethods I can use with the textbox. I am currently able to restrict entries into the textbox to be positive number but have problems with the negative sign of the negative number. For example , I need to restrict the entry of the negative sign to be only possible as the first entry in the textbox but is unable to do so. How can I do this? Is it possible to know the cursor position in the textbox?

    I thank you


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Validation of entries in textbox

    do it in keypress event, i.e. say text1 is the textbox

    if len(text1) > 0 then
    if asciikey = asc("-") then
    asciikey = 0
    end if
    end if

    HTH

    cksiow
    http://vblib.virtualave.net - share our codes


  3. #3
    Join Date
    Apr 2001
    Location
    India
    Posts
    3

    Re: Validation of entries in textbox

    this is for you my dear friend


    sudheer

  4. #4
    Join Date
    Nov 2005
    Posts
    56

    Re: Validation of entries in textbox

    Quote Originally Posted by cksiow
    do it in keypress event, i.e. say text1 is the textbox

    if len(text1) > 0 then
    if asciikey = asc("-") then
    asciikey = 0
    end if
    end if

    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

    something wrong this this codes? seem to have error in the first line

  5. #5
    Join Date
    Sep 2005
    Location
    Delhi, INDIA
    Posts
    237

    Re: Validation of entries in textbox

    Quote Originally Posted by WeeBeng
    I wish to be able to enter only positive or negative numbers into a textbox.How can I go about doing this? Is there any inherent vbmethods I can use with the textbox. I am currently able to restrict entries into the textbox to be positive number but have problems with the negative sign of the negative number. For example , I need to restrict the entry of the negative sign to be only possible as the first entry in the textbox but is unable to do so. How can I do this? Is it possible to know the cursor position in the textbox?

    I thank you
    Hi

    try out this and definately you will thanks me for this ;;


    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    	If KeyAscii >= 48 And KeyAscii <= 57 Then
    	Else
    		If KeyAscii = 45 And Val(Text1.Text) = 0 Then
    		Else
    			KeyAscii = 0
    		End If
    	End If
    	
    End Sub
    So Enjoy and Happy Coding !

    Regards Rahul !
    I'M BACK AGAIN !!
    -------------------------------------------------------------------------
    enjoy the VB !
    If any post helps you, please rate that.
    Always try to findout the Solutions, instead just discussing the problem and its scope!

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Validation of entries in textbox

    I use this in the keypress sub
    Code:
    Select Case KeyAscii
        Case 1 To 7
            KeyAscii = 0
        Case 9 To 44
            KeyAscii = 0
        Case 47
            KeyAscii = 0
        Case 58 To 255
            KeyAscii = 0
    End Select
    Using the select case method i get a bit more control on what i want to let through..

    Gremmy....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

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