creer un textbox numeric - VB/VBA/VBS - Programmation
MarshPosté le 14-03-2005 à 19:11:56
voila je voudrais creer un nouveau control un textbox qui soit : couleur info text centré aucun texte par defaut police etc...
ceci j'ai reussi à le faire mais je voudrait qu'il n'accepte que des chiffre ou une virgule.
voici mon code dite moi ou ca coince vb.net ne me donne aucune erreur mais accepte tout les caracteres.
Public Class numeric Inherits System.Windows.Forms.TextBox
Private numeric As Boolean
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Textnumeric As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Textnumeric = New System.Windows.Forms.TextBox ' 'Textnumeric ' Me.Textnumeric.Location = New System.Drawing.Point(17, 17) Me.Textnumeric.Name = "Textnumeric" Me.Textnumeric.TabIndex = 0 Me.Textnumeric.Text = ""
End Sub Public Property num() As Boolean Get num = numeric End Get
Set(ByVal Value As Boolean) numeric = Value
If Value = True Then Me.BackColor = System.Drawing.SystemColors.Info Me.Text = "" Me.TextAlign = HorizontalAlignment.Center Me.Refresh() End If End Set
End Property
#End Region
Private Sub Textnumeric_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textnumeric.KeyPress If Asc(e.KeyChar) = 46 Then If InStr(1, Textnumeric.Text, "," ) > 0 Then e.Handled = True Exit Sub End If Textnumeric.SelectedText = "," End If
' SI la touche appuyée est autre que : un chiffre, une touche contrôle, une "," ou un "-" ' ALORS la touche est sans effet. If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) _ Or e.KeyChar = "," Or e.KeyChar = "-" ) Then If Asc(e.KeyChar) = 46 Then e.Handled = True Exit Sub End If e.Handled = True Exit Sub End If End Sub End Class
Marsh Posté le 14-03-2005 à 19:11:56
voila je voudrais creer un nouveau control un textbox qui soit :
couleur info
text centré
aucun texte par defaut
police etc...
ceci j'ai reussi à le faire
mais je voudrait qu'il n'accepte que des chiffre ou une virgule.
voici mon code dite moi ou ca coince vb.net ne me donne aucune erreur mais accepte tout les caracteres.
Public Class numeric
Inherits System.Windows.Forms.TextBox
Private numeric As Boolean
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Textnumeric As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Textnumeric = New System.Windows.Forms.TextBox
'
'Textnumeric
'
Me.Textnumeric.Location = New System.Drawing.Point(17, 17)
Me.Textnumeric.Name = "Textnumeric"
Me.Textnumeric.TabIndex = 0
Me.Textnumeric.Text = ""
End Sub
Public Property num() As Boolean
Get
num = numeric
End Get
Set(ByVal Value As Boolean)
numeric = Value
If Value = True Then
Me.BackColor = System.Drawing.SystemColors.Info
Me.Text = ""
Me.TextAlign = HorizontalAlignment.Center
Me.Refresh()
End If
End Set
End Property
#End Region
Private Sub Textnumeric_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textnumeric.KeyPress
If Asc(e.KeyChar) = 46 Then
If InStr(1, Textnumeric.Text, "," ) > 0 Then
e.Handled = True
Exit Sub
End If
Textnumeric.SelectedText = ","
End If
' SI la touche appuyée est autre que : un chiffre, une touche contrôle, une "," ou un "-"
' ALORS la touche est sans effet.
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) _
Or e.KeyChar = "," Or e.KeyChar = "-" ) Then
If Asc(e.KeyChar) = 46 Then
e.Handled = True
Exit Sub
End If
e.Handled = True
Exit Sub
End If
End Sub
End Class