Results 1 to 4 of 4

Thread: numeric only textbox

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    numeric only textbox

    i thought .net had a property to do this but i can't find it. what is the .net way to do it?
    Magiaus

    If I helped give me some points.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    with keypress event

    Code:
    Private Sub OnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    
       e.Handled = (Not Char.IsControl(e.KeyChar)) AndAlso _
         (Not Char.IsNumber(e.KeyChar)) AndAlso _
         (Not (Char.IsPunctuation(e.KeyChar) And _
         (((e.KeyChar = ".") And
         (Text.IndexOf(".") = -1)) Or _
         ((e.KeyChar = "-") And (SelectionStart = 0)))))
    
    End Sub
    that allows decimals and negative numbers also
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    also take not that if it is something you may use more often, you can create a class that inherits TextBox and change Private to Protected Overrides for the event. Now you have a reusable numeric only textbox class that retains all the orginal textbox functionality

    Isnt inheritance awesome?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    ah

    .handled ok thanks bro
    Magiaus

    If I helped give me some points.

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