Source: http://amronbadriza.blogspot.com/2012/06/cara-memberi-burung-twitter-terbang-di.html#ixzz2Cjh0y8gV

Minggu, 06 Januari 2013

Numeric Textbox 2 Decimal places

I am hoping this is not inappropriate to do on here if it is I apologize in advance and will let the thread die a slow agonizing death.
Here goes:
I have a little TextBox app that's supposed to only allow numeric’s positive and negative and allow 2 decimal places. I have tested it and it seems to work as intended. What I'd like is for you all to try and break it. The max characters functionality needs some work but it's not the focus of my TextBox.
Numbers_Only_Form
  1. 'K. W. 4/19/2012
  2. 'TextBox Control For
  3. 'Numerics/Money entry
  4. '1 Decimal Point
  5. '2 Decimal Places
  6. 'Allows The BackSpace
  7. 'Handles enter key by Moving to the Next control
  8. 'Set Max characters checked - this sets it to 4 if not checked allows 10 characters
  9. 'The button just multiplies the textbox value by 5
  10. Public Class Form1
  11. Private maxSize As Boolean = False
  12. Private numMaxSize As Integer
  13. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  14. If T1.Text <> "" Then
  15. If CDbl(T1.Text) > 0 Or CDbl(T1.Text) < 0 Then
  16. MsgBox(CDbl(T1.Text) * 5)
  17. T1.Clear()
  18. End If
  19. End If
  20. GetNextControl(Button1, False).Focus()
  21. End Sub
  22. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  23. 'Add Handler for all the Numeric only TextBoxes on the Form
  24. For Each ctrl As Control In Me.Controls
  25. If TypeOf ctrl Is TextBox Then
  26. AddHandler ctrl.KeyPress, AddressOf Boxkeypress
  27. End If
  28. Next ctrl
  29. T1.MaxLength = 10
  30. End Sub
  31. Private Sub Boxkeypress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
  32. Dim txt As TextBox = CType(sender, TextBox)
  33. If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
  34. If e.KeyChar = "." And txt.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal point
  35. If e.KeyChar = "-" And txt.SelectionStart = 0 Then e.Handled = False 'allow negative number
  36. 'Enter key move to next control
  37. If e.KeyChar = Chr(13) Then
  38. GetNextControl(txt, True).Focus()
  39. 'If only a decimal point is in the box clear TextBox
  40. If e.KeyChar = Chr(13) And txt.Text = "." Then txt.Clear()
  41. Exit Sub
  42. End If
  43. Dim i As Integer = txt.Text.IndexOf(".")
  44. Dim len As Integer = txt.Text.Length
  45. 'Allow only 2 Decimal places
  46. If Not e.Handled Then
  47. If i = -1 Then
  48. e.Handled = False
  49. Else
  50. If (len - i) > 2 Then e.Handled = True
  51. End If
  52. End If
  53. If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
  54. End Sub
  55. Private Sub chkMaxSize_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkMaxSize.CheckedChanged
  56. If chkMaxSize.Checked Then
  57. maxSize = True
  58. T1.MaxLength = 4
  59. Else
  60. maxSize = False
  61. T1.MaxLength = 10
  62. End If
  63. End Sub
  64. End Class
  65.  
  66.  
  67. sumber : http://www.daniweb.com/software-development/vbnet/threads/434461/numeric-textbox-2-decimal-places

Tidak ada komentar:

Posting Komentar