Imports System.Drawing
Imports System.Windows.Forms
<ToolboxBitmap(GetType(TextBox))> _
Public Class AdvanceTextbox : Inherits TextBox
'รับค่า/แสดงจำนวนจุดทศนิยม
Public Enum TextBoxDisplayPrecisionScale As Byte
None = 0
One = 1
Two = 2
Tree = 3
Four = 4
End Enum
#Region " Variables "
Private _DisplayPrecisionScale As TextBoxDisplayPrecisionScale
#End Region
#Region " Properties "
<System.ComponentModel.DefaultValue(GetType(TextBoxDisplayPrecisionScale), "None"), System.ComponentModel.Category("Appearance")> _
Public Property DisplayPrecisionScale() As TextBoxDisplayPrecisionScale
Get
Return _DisplayPrecisionScale
End Get
Set(ByVal Value As TextBoxDisplayPrecisionScale)
_DisplayPrecisionScale = Value
MyBase.TextAlign = HorizontalAlignment.Right 'จัดตำแหน่งชิดขวา
Dim dblSubMybaseText As Double
If String.IsNullOrEmpty(MyBase.Text) OrElse Not IsNumeric(MyBase.Text) Then
dblSubMybaseText = 0
Else
dblSubMybaseText = CDbl(MyBase.Text)
End If
Select Case Me._DisplayPrecisionScale
Case TextBoxDisplayPrecisionScale.One
MyBase.Text = String.Format("{0:N1}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Two
MyBase.Text = String.Format("{0:N2}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Tree
MyBase.Text = String.Format("{0:N3}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Four
MyBase.Text = String.Format("{0:N4}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.None
MyBase.Text = String.Format("{0:N0}", dblSubMybaseText)
End Select
End Set
End Property
#End Region
#Region " Constructor "
Public Sub New()
End Sub
#End Region
Protected Overrides Sub OnGotFocus(e As System.EventArgs)
MyBase.ForeColor = Color.Orange
'More...
MyBase.OnGotFocus(e)
End Sub
Protected Overrides Sub OnLostFocus(e As System.EventArgs)
MyBase.ForeColor = Color.Black
'More...
MyBase.OnLostFocus(e)
End Sub
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
Dim dblSubMybaseText As Double
If String.IsNullOrEmpty(MyBase.Text) OrElse Not IsNumeric(MyBase.Text) Then
dblSubMybaseText = 0
Else
dblSubMybaseText = CDbl(MyBase.Text)
End If
Select Case Me._DisplayPrecisionScale
Case TextBoxDisplayPrecisionScale.One
MyBase.Text = String.Format("{0:N1}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Two
MyBase.Text = String.Format("{0:N2}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Tree
MyBase.Text = String.Format("{0:N3}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Four
MyBase.Text = String.Format("{0:N4}", dblSubMybaseText)
Case Else
MyBase.Text = String.Format("{0:N0}", dblSubMybaseText)
End Select
End Sub
'ปิดเสียง ปี๊ด เมื่อกดปุ่ม Enter
Private Sub SetBeepOff(ByVal senser As Control, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
SendKeys.Send("{Tab}")
End If
End Sub
End Class