numericTextbox-VBForums
Results 1 to 1 of 1

Thread: numericTextbox

  1. #1

    Thread Starter
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    19,056

    numericTextbox

    heres my numericTextbox. it allows only positive or negative decimal user input

    vb.net Code:
    1. Public Class numericTextbox
    2.  
    3.     Inherits TextBox
    4.  
    5.     Const WM_PASTE As Integer = &H302
    6.  
    7.     Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    8.         Dim strText As String = Me.Text
    9.         strText = strText.Remove(Me.SelectionStart, Me.SelectionLength)
    10.         strText = strText.Insert(Me.SelectionStart, e.KeyChar)
    11.         e.Handled = CBool(strText.LastIndexOf("-") > 0) Or Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse (e.KeyChar = "."c And Not Me.Text.Contains(".") Or e.KeyChar = "."c And Me.SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And Me.SelectionStart = 0))
    12.         MyBase.OnKeyPress(e)
    13.     End Sub
    14.  
    15.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    16.         If m.Msg = WM_PASTE Then
    17.             Dim strText As String = Me.Text
    18.             strText = strText.Remove(Me.SelectionStart, Me.SelectionLength)
    19.             strText = strText.Insert(Me.SelectionStart, Clipboard.GetText)
    20.             Dim result As Decimal = 0
    21.             If Not Decimal.TryParse(strText, result) Then
    22.                 Return
    23.             End If
    24.         End If
    25.         MyBase.WndProc(m)
    26.     End Sub
    27.  
    28. End Class
    Attached Files Attached Files
    Last edited by .paul.; Feb 3rd, 2008 at 07:54 AM. Reason: added class file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.