Results 1 to 6 of 6

Thread: VB>NET : Allow a Textbox only to Accept Numeric [SOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    6

    Question VB>NET : Allow a Textbox only to Accept Numeric [SOLVED]

    Hi all

    Does anyone know how to Allow a User only to Type numeric characters only into a textbox.

    I know in VB6 you could catch the char and set it to '0' if not valid
    Code:
    If not KeyAscii = '27' then
    Code:
    keyascii = 0
    Code:
    End If
    But how do you make it work in VB.NET ?

    I have tried adding the Numberbox control to my app but somehow I get errors with this control when exiting the App
    Last edited by StaticInc; Nov 27th, 2003 at 04:38 AM.
    'When all esle fails
    On Error Resume Next

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    like this...
    VB Code:
    1. [Color=Blue]Private[/color] [Color=Blue]Sub[/color] TextBox1_KeyPress([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] [Color=Blue]Object[/color], [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.Windows.Forms.KeyPressEventArgs) [Color=Blue]Handles[/color] TextBox1.KeyPress
    2.         [Color=Blue]If[/color] e.KeyChar.IsNumber(e.KeyChar) [Color=Blue]Then
    3. [/color]            e.Handled = [Color=Blue]False
    4. [/color]        [Color=Blue]Else
    5. [/color]            e.Handled = [Color=Blue]True
    6. [/color]        [Color=Blue]End[/color] [Color=Blue]If
    7. [/color]    [Color=Blue]End[/color] [Color=Blue]Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    6
    I knew I forgot to check something

    Thanx It Works
    'When all esle fails
    On Error Resume Next

  4. #4
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120
    But if i have many Text boxs then do i have to write in every text box keypress event

    Can we call some function in formkeypress event
    with Regards
    sameer Mulgaonkar

  5. #5
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    better make an array of those txt boxes or declare them dynamically and then u can use that
    Regards

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you need to make a Handler for all the textboxes like this then...
    VB Code:
    1. [Color=Blue]Private[/color] [Color=Blue]WithEvents[/color] txtBox [Color=Blue]As[/color] TextBox
    2.  
    3.     [Color=Blue]Private[/color] [Color=Blue]Sub[/color] Form1_Load([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] [Color=Blue]MyBase[/color].Load
    4.         [Color=Blue]Dim[/color] ctl [Color=Blue]As[/color] Control
    5.         [Color=Blue]For[/color] [Color=Blue]Each[/color] ctl [Color=Blue]In[/color] [Color=Blue]Me[/color].Controls
    6.             [Color=Blue]If[/color] [Color=Blue]TypeOf[/color] ctl [Color=Blue]Is[/color] TextBox [Color=Blue]Then
    7. [/color]                [Color=Blue]AddHandler[/color] ctl.KeyPress, [Color=Blue]AddressOf[/color] txtBox_KeyPress
    8.             [Color=Blue]End[/color] [Color=Blue]If
    9. [/color]        [Color=Blue]Next
    10. [/color]    [Color=Blue]End[/color] [Color=Blue]Sub
    11.  
    12. [/color]    [Color=Blue]Private[/color] [Color=Blue]Sub[/color] txtBox_KeyPress([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] [Color=Blue]Object[/color], [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.Windows.Forms.KeyPressEventArgs) [Color=Blue]Handles[/color] txtBox.KeyPress
    13.         [Color=Blue]If[/color] e.KeyChar.IsNumber(e.KeyChar) [Color=Blue]Then
    14. [/color]            e.Handled = [Color=Blue]False
    15. [/color]        [Color=Blue]Else
    16. [/color]            e.Handled = [Color=Blue]True
    17. [/color]        [Color=Blue]End[/color] [Color=Blue]If
    18. [/color]    [Color=Blue]End[/color] [Color=Blue]Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

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