Results 1 to 7 of 7

Thread: [2005] NumericTextbox

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    [2005] NumericTextbox

    Dear All,
    I am trying to develope a Numeric text control box as follows.But this not working properly.Can somebody help.
    VB Code:
    1. Public Class NumericTextboxControl
    2.     Inherits System.Windows.Forms.TextBox
    3.     Private _Handled As Boolean = False
    4.  
    5.     Protected Overrides Function IsInputkey(ByVal Keychar As System.Windows.Forms.Keys) As Boolean
    6.         Select Case Keychar
    7.             Case Keys.NumPad0 To Keys.NumPad9 'Allow Numberpad 0-9
    8.                 Return _Handled = True
    9.             Case Keys.Back 'Allow Backspace key
    10.                 Return _Handled = True
    11.             Case Keys.D0 To Keys.D9  'Allow Decimal 0-9
    12.                 Return _Handled = True
    13.             Case Else
    14.                 Return _Handled = False
    15.         End Select
    16.     End Function
    17.     Public Property Handled() As Boolean
    18.         Get
    19.             Return _Handled
    20.         End Get
    21.         Set(ByVal value As Boolean)
    22.             _Handled = False
    23.         End Set
    24.     End Property
    25.  
    26. End Class
    Last edited by danasegarane; Feb 17th, 2007 at 07:57 AM.
    Please mark you thread resolved using the Thread Tools as shown

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] NumericTextbox

    Give us a bit of a clue....... what's it not doing? what results are you getting?
    If my post helps , please feel free to rate it

  3. #3

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] NumericTextbox

    It allows both numeric and alphabets.I want to restrict to only Numbers.
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] NumericTextbox

    I'm assuming your creating a user control ?

    This code will allow only numeric,backspace and periods, it also overrides the text property to allow it to be read in the host application.

    The code needs modifying to stop multiple periods being entered.

    VB Code:
    1. 'override the text property to allow it to be read in the
    2.     'host application
    3.     Public Overrides Property Text() As String
    4.         Get
    5.             Return NumericTextBox.Text
    6.         End Get
    7.         Set(ByVal Value As String)
    8.         End Set
    9.     End Property
    10.  
    11.  
    12.     Private Sub NumericTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NumericTextBox.KeyPress
    13.         'basically this allows numbers,backspace and period
    14.         'need to modify to stop double periods being entered
    15.         e.Handled = Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) AndAlso Not e.KeyChar = "."
    16.     End Sub
    If my post helps , please feel free to rate it

  5. #5
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] NumericTextbox

    This will stop multiple periods being entered....

    VB Code:
    1. Private Sub NumericTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NumericTextBox.KeyPress
    2.         'basically this allows numbers,backspace and period
    3.         If e.KeyChar = "." AndAlso NumericTextBox.Text.IndexOf(".") > 0 Then
    4.             e.Handled = True
    5.         Else
    6.             e.Handled = Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) AndAlso Not e.KeyChar = "."
    7.         End If
    8.     End Sub
    If my post helps , please feel free to rate it

  6. #6

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] NumericTextbox

    Dear Tinbeard,
    Where do I place this codings.Can you please brief?
    Please mark you thread resolved using the Thread Tools as shown

  7. #7
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] NumericTextbox

    Basically create a new user control, add a textbox to it then place the code within the class. I called my textbox 'NumericTextBox' but you can call it what you like, just modify the names in the code where applicable.
    If my post helps , please feel free to rate it

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