Cijfervak: code

- Om te starten nemen we een tekstvak. De tekst wordt
rechts uitgelijnd.
- We moeten het tekstvak even breed maken als de
control zelf.
- De code:
Public Class cijferbox
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
'...
Private Sub resizeme() 'in VS 2008 kan je dit doen door te docken
txtBox.Top = 0
txtBox.Left = 0
txtBox.Width = Me.Width
txtBox.Height = Me.Height
End Sub
Public Sub New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
resizeme()
decimaal = Mid(CStr(3 / 2), 2, 1) 'het decimaalteken
End Sub
'...
#End Region
Dim decimaal As String
Dim decimaalteken As Boolean 'is het decimaalteken reeds geplaatst?
Private Sub txtBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtBox.KeyPress
If Not IsNumeric(txtBox.Text & e.KeyChar.ToString) Then
e.Handled = False
End If
Exit Sub
If e.KeyChar.ToString = decimaal Then
If decimaalteken Then
e.Handled = False 'beschouw als afgewerkt
Else
e.Handled = True
End If
decimaalteken = False
End If
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
Public Property waarde() As Double
Get
Return CDbl(txtBox.Text)
End Get
Set(ByVal Value As Double)
txtBox.Text = CStr(Value)
End Set
End Property
Private Sub txtBox_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtBox.TextChanged
End Sub
Private Sub cijferbox_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cijferbox_SizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.SizeChanged
With Me
txtBox.Top = 0
txtBox.Left = 0
txtBox.Width = .Width
txtBox.Height = .Height
End With
End Sub
End Class
Een alternatieve manier om na te gaan of de invoer OK is, is om de invoer
even toe te laten, een 0 toe te voegen en te zien of het nog steeds een getal is.
Zie Cijferbox.
[Controls en klassen in .NET] -
[VB Web] - [hlrnet]
- [copyright]