TextBox limit number of digit to enter

Discussion in 'Questions (Windows Mobile)' started by mike2ppc, Aug 11, 2008.

  1. mike2ppc

    mike2ppc Member Licensed User

    Hello,

    does somebody know to limit in a TextBox possible digits
    e.g.

    Form1 - TextBox1

    Sub TextBox1_KeyPress (key)
    If StrLength(textbox1.Text)>3 Then
    key=""
    End If
    End Sub

    I want to limit the Textbox to max. 3 digits, is this possible.

    Best Regards

    Michael
     
  2. taximania

    taximania Well-Known Member Licensed User

    This was not done by me.
    I think it's agraham who wrote this code.
    I think the number 2 is correct for your needs.

    Code:
    Sub TextBox1_KeyPress (key)
        
    If StrLength( textbox1.Text) >=[COLOR="Red"]2[/COLOR] Then   
            
    If Asc(key)>31 Then
                ss = Textbox1.SelectionStart
                Textbox1.Text= SubString(Textbox1.Text,
    0,[COLOR="red"]2[/COLOR])
                Textbox1.SelectionStart = ss
            
    End If
        
    End If
    End Sub
     
  3. specci48

    specci48 Well-Known Member Licensed User

    If you want to do it a more elegant way, just have a look at filippos fgcontrols library.

    The fgTextBox provides a MaxLenght and a OnlyNumber property.


    specci48
     
  4. mike2ppc

    mike2ppc Member Licensed User

    Thanks for all the idea,
    my final solution is now

    On Form1 are 2 TextBox Controls:

    Sub App_Start
    AddEvent("TextBox1",KeyPress,"TextBox_maxC5_KeyPress")
    AddEvent("TextBox2",KeyPress,"TextBox_maxC5_KeyPress")
    Form1.Show
    End Sub


    Sub TextBox_maxC5_KeyPress (key)
    If StrLength(Control(Sender))=5 AND _
    ( IsLetter(key) OR IsNumber(key) ) Then
    Control(Sender).IgnoreKey
    End If
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice