Text Box Event Problem

vks.gautam1

Well-known member
Joined
Oct 10, 2008
Messages
78
Location
Chandigarh, India
Programming Experience
Beginner
:)
VB.NET:
sub Textbox1_keypress()

if char.isletter[B](e.keychar)[/B] then

msgbox("You Must Enter Numeric values ")

[B]e.handled=True[/B]
end if
end sub

Text Box will accept only Numeric values due to if condition & keypress event.

What is this.? e.keychar & e.handled=True
Pls describe in Simple Manner
 
Last edited:
:)
VB.NET:
sub Textbox1_keypress()

if char.isletter[B](e.keychar)[/B] then

msgbox("You Must Enter Numeric values ")

[B]e.handled=True[/B]
end if
end sub

Text Box will accept only Numeric values due to if condition & keypress event.

What is this.? e.keychar & e.handled=True
Pls describe in Simple Manner
Why not use a control made for this instead of wasting time forcing a TB to limit user input? http://www.vbdotnetforums.com/windows-forms/28288-numericupdown.html
 
Drop a NUD onto the form, set it's Maximum, Minimum and DecimalPlaces properties and there you go. You can easily control the range of allowed numbers and anything else you need. Be sure to use the Value property when retrieving the number from the control (it's already as type decimal, no string converting)
 
As the other posters explained there is a control that will allow only numeric values for input. But considering you say this is for a class a may be forced to use the textbox, here is a brief explaination.

Your texbox_KeyPress event is triggered whenever you press a key while the textbox has focus. The key you pressed is passed as one of the events parameters (e).

e.KeyChar will show you what key was pressed. Try msgbox e.KeyChar to see.

e.Handled = True is telling the textbox not to allow the letter typed to be entered.


VB.NET:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal [COLOR="Red"]e As System.Windows.Forms.KeyPressEventArgs[/COLOR]) Handles TextBox1.KeyPress

        If Char.IsLetter(e.KeyChar) Then
            MsgBox("You must enter numeric values")
            e.Handled = True
        End If

    End Sub
 
vks.gautam1 glad it helps. If you still do not fully understand both the NumericUpDown control and also control events and parameters along with key capturing just let me know and I can create some working examples for you.
 
Back
Top