Newsgroup: it.comp.lang.visual-basic
Titolo: key control

I would like to use txtbox to type into number. But I would like to limit 
the input in value (0-9), what can I do? Thank you very much!

 the input in value (0-9), what can I do? Thank you very much!
Try with this:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 47 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
@Alex
-- 
Posted via Mailgate.ORG Server - 
http://www.Mailgate.ORG


Let me suggest the following "tricky" code, since it doesn't block backspace
and cut/copy/paste keys:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32.dll" Alias
"SetWindowLongA" ( _
     ByVal hwnd As Long, _
     ByVal nIndex As Long, _
     ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER = &H2000&
Public Sub SetTextBoxNumeric(ByVal txt As TextBox)
Dim wndStyle    As Long
Dim ret         As Long
    wndStyle = GetWindowLong(txt.hwnd, GWL_STYLE)
    wndStyle = wndStyle Or ES_NUMBER
    ret = SetWindowLong(txt.hwnd, GWL_STYLE, wndStyle)
End Sub
Private Sub Form_Load()
    SetTextBoxNumeric Text1
End Sub
But notice that none of the above methods will prevent you to actually paste
non-numeric text using the context menu...
Bye
Raf


[CUT]
Maybe you're more interested in a 
VB.NET
 version of the above "trick"...
Well, you might think to extend TextBox class in order to obtain a
numeric-only Text Box control:
Public Class NumericTextBox Inherits System.Windows.Forms.TextBox
    Protected Overrides ReadOnly Property CreateParams() _
         As System.Windows.Forms.CreateParams
        Get
            Private Const ES_NUMBER As Integer = &H2000
            'Extend the CreateParams property of the Button class.
            Dim cp As System.Windows.Forms.CreateParams = _
             MyBase.CreateParams
            'Update the button Style.
            cp.Style = cp.Style Or ES_NUMBER
            Return cp
        End Get
    End Property
End Class
This new custom control can be used in place of ordinary textbox every time
you need to limit user input to digits.
Unfortunately I couldn't test the above code since .NET is not installed on
the PC I'm writing with.
Bye
Raf

This is my idea:
it tracks the key press event 
(you may use the BackWard key too)
and 
it tracks the Copy/Paste event (or at least it should).
How the Copy/Paste tracking event works:
the content ot the text to be copied 
is tracked in the TextChanged event
and if it is not a numeric content, 
the TextChanged event restores the previous content back
(a String variabile has declared for this).
Be aware that   -2345  and   567,77   and   6789.987     
are considered numeric content.
All this needs more testing.
Se anche qualcuno di voi vuole dargli una occhiata, ragazzi...
Note:
there is an appropriate Italian Net newsgroup 
called 'microsoft.public.it.dotnet.vb'
you better post the Net questions there.
and
you surely may find *tons* of tested code on the web
regarding the numeric input validation.
    Dim oldTextContent As String
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If (AscW(e.KeyChar) < 47 Or AscW(e.KeyChar) > 57) And
AscW(e.KeyChar) <> 8 Then
            e.KeyChar = ChrW(0)
        End If
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged
        If IsNumeric(TextBox1.Text) = False And TextBox1.Text <> "" Then
            TextBox1.Text = oldTextContent
        Else
            oldTextContent = TextBox1.Text
        End If
    End Sub

"Rafunk" <rafun...@NOTHANKSlibero.it> wrote in message
news:43ca1c78$0$1064$
4faf...@reader2.news.tin.it
[CUT]

May be a full solution Subclassing in this way:
Private Sub Form_Load()
Dim style As Long
    ' Get the current style.
    style = GetWindowLong(Text1.hWnd, GWL_STYLE)
    ' Add ES_NUMBER to the style.
    SetWindowLong Text1.hWnd, GWL_STYLE, style Or ES_NUMBER
    ' Subclass to ignore the context menu.
    OldWindowProc = SetWindowLong( _
        Text1.hWnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub
Public Function NewWindowProc(ByVal hWnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
    If (msg <> WM_PASTE) And (msg <> WM_CONTEXTMENU) Then
        NewWindowProc = CallWindowProc( _
            OldWindowProc, hWnd, msg, wParam, _
            lParam)
    End If
End Function
