منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
تكست بوكس يسمح بإدخال أرقام فقط أو حروف فقط - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb)
+-- قسم : قسم لغة الفيجوال بيسك VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=182)
+--- قسم : قسم مكتبة اكواد VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=185)
+--- الموضوع : تكست بوكس يسمح بإدخال أرقام فقط أو حروف فقط (/showthread.php?tid=22047)



تكست بوكس يسمح بإدخال أرقام فقط أو حروف فقط - silverlight - 09-10-17

الكلاس التالي عبارة عن تكست بوكس يسمح بإدخال أرقام فقط أو حروف فقط و الكلالس ثم توريثه من TextBox الموجود في الدوت نت

الكود :

PHP كود :
Imports System.Globalization

Public Class CairoTextBox
    Inherits TextBox

    Private _textType 
As TextTypes TextTypes.Numbers
    Private _message 
As String String.Empty
 
   Private tip As ToolTip = New ToolTip()

 
   Private Sub ShowTip()
 
       Me.tip.AutomaticDelay 1
        Me
.tip.AutoPopDelay 1000
        Me
.tip.SetToolTip(MeMe._message)
 
       Me.tip.Show(Me._messageMeMyBase.Width 2MyBase.Height)
 
   End Sub

    Private Sub HideTip
()
 
       Me.tip.SetToolTip(MeString.Empty)
 
       Me.tip.AutomaticDelay 1000
        Me
.tip.Hide(Me)
 
   End Sub

    Protected Overrides Sub OnLostFocus
(As EventArgs)
 
       If Not MyBase.DesignMode Then
            Me
.HideTip()
 
       End If
 
       MyBase.OnLostFocus(e)
 
   End Sub

    Protected Overrides Sub OnTextChanged
(As EventArgs)
 
       MyBase.OnTextChanged(e)
 
       Me.ShowText()
 
   End Sub

    Private Sub ShowText
()
 
       If Not MyBase.DesignMode Then
            Dim selection 
As Integer MyBase.SelectionStart
            Dim bool 
As Integer 0
            Dim txt 
As String String.Empty
 
           Dim s As String Me.Text

            For i 
As Integer 0 To s.Length 1
                Dim current 
As Char s(i)
 
               Select Case Me._textType
                    Case TextTypes
.Numbers
                        If Char
.IsNumber(currentThen
                            txt 
+= current
                        Else
                            bool 
+= 1
                        End 
If

 
                   Case TextTypes.Characters
                        If Not Char
.IsNumber(currentThen
                            txt 
+= current
                        Else
                            bool 
+= 1
                        End 
If
 
               End Select

            Next

            If bool 
0 Then
                Me
.Text String.Format(CultureInfo.CurrentCulture"{0}", {txt})
 
               MyBase.SelectionStart selection bool
                Me
.ShowTip()
 
               Return
            End 
If
 
           Me.HideTip()

 
       End If
 
   End Sub

    Public Property TextType 
As TextTypes
        Get
            Return Me
._textType
        End Get
        Set
(value As TextTypes)
 
           Me._textType value
        End Set
    End Property

    Public Property TipMessage 
As String
        Get
            Return Me
._message
        End Get
        Set
(value As String)
 
           Me._message value
        End Set
    End Property

End 
Class

Public 
Enum TextTypes
    Numbers
    Characters
End Enum 

أضف الكلاس لمشروعك
قم بعمل Build لللكلاس
أضف التكست بوكس للفورم

اضبط الحواص التالية:
حدد نوع المدخلات إما حرزف أو أرقام من الصفة TextType
خدد نوع الرسالة التي تريد إظهارها في حالة الخطأ من الصفة TipMessage
مثلا لو نوع المدحلات أرقام فقط ممكن تكون رسالة الخطأ ...... مسموح إدخال أرقام فقط


الكلاس أعلاه ممكن كتابته علي هيئة كلاسان
الكلاس الأول يقبل أرقام فقط
PHP كود :
Imports System.Globalization

Public Class NumbersTextBox
    Inherits TextBox

    Private tip 
As ToolTip = New ToolTip()
 
   Private _message As String "مسموح إدخال أرقام فقط"


 
   Private Sub ShowTip()
 
       Me.tip.AutomaticDelay 1
        Me
.tip.AutoPopDelay 1000
        Me
.tip.SetToolTip(MeMe._message)
 
       Me.tip.Show(Me._messageMeMyBase.Width 2MyBase.Height)
 
   End Sub

    Private Sub HideTip
()
 
       Me.tip.SetToolTip(MeString.Empty)
 
       Me.tip.AutomaticDelay 1000
        Me
.tip.Hide(Me)
 
   End Sub

    Protected Overrides Sub OnLostFocus
(As EventArgs)
 
       If Not MyBase.DesignMode Then
            Me
.HideTip()
 
       End If
 
       MyBase.OnLostFocus(e)
 
   End Sub

    Protected Overrides Sub OnTextChanged
(As EventArgs)
 
       MyBase.OnTextChanged(e)

 
       If Not MyBase.DesignMode Then
            Dim selection 
As Integer MyBase.SelectionStart
            Dim bool 
As Integer 0
            Dim txt 
As String String.Empty
 
           Dim s As String Me.Text
            For i 
As Integer 0 To s.Length 1
                Dim current 
As Char s(i)
 
               If Char.IsNumber(currentThen
                    txt 
+= current
                Else
                    bool 
+= 1
                End 
If
 
           Next

            If bool 
0 Then
                Me
.Text String.Format(CultureInfo.CurrentCulture"{0}", {txt})
 
               MyBase.SelectionStart selection bool
                Me
.ShowTip()
 
               Return
            End 
If
 
           Me.HideTip()

 
       End If
 
   End Sub

    Public Property TipMessage 
As String
        Get
            Return Me
._message
        End Get
        Set
(value As String)
 
           Me._message value
        End Set
    End Property

End 
Class 

الكلاس الثاني يقبل حروف فقط

PHP كود :
Imports System.Globalization

Public Class CharactersTextBox
    Inherits TextBox

    Private tip 
As ToolTip = New ToolTip()
 
   Private _message As String "مسموح إدخال حروف فقط"

 
   Private Sub ShowTip()
 
       Me.tip.AutomaticDelay 1
        Me
.tip.AutoPopDelay 1000
        Me
.tip.SetToolTip(MeMe._message)
 
       Me.tip.Show(Me._messageMeMyBase.Width 2MyBase.Height)
 
   End Sub

    Private Sub HideTip
()
 
       Me.tip.SetToolTip(MeString.Empty)
 
       Me.tip.AutomaticDelay 1000
        Me
.tip.Hide(Me)
 
   End Sub

    Protected Overrides Sub OnLostFocus
(As EventArgs)
 
       If Not MyBase.DesignMode Then
            Me
.HideTip()
 
       End If
 
       MyBase.OnLostFocus(e)
 
   End Sub

    Protected Overrides Sub OnTextChanged
(As EventArgs)
 
       MyBase.OnTextChanged(e)

 
       If Not MyBase.DesignMode Then
            Dim selection 
As Integer MyBase.SelectionStart
            Dim bool 
As Integer 0
            Dim txt 
As String String.Empty
 
           Dim s As String Me.Text
            For i 
As Integer 0 To s.Length 1
                Dim current 
As Char s(i)
 
               If Not Char.IsNumber(currentThen
                    txt 
+= current
                Else
                    bool 
+= 1
                End 
If
 
           Next

            If bool 
0 Then
                Me
.Text String.Format(CultureInfo.CurrentCulture"{0}", {txt})
 
               MyBase.SelectionStart selection bool
                Me
.ShowTip()
 
               Return
            End 
If
 
           Me.HideTip()

 
       End If
 
   End Sub

    Public Property TipMessage 
As String
        Get
            Return Me
._message
        End Get
        Set
(value As String)
 
           Me._message value
        End Set
    End Property

End 
Class 


أتمني أن يكون الكود مفيدا لكم


RE: تكست بوكس يسمح بإدخال أرقام فقط أو حروف فقط - سعود الشامان - 15-10-17

كلاس مهم جداً

شكرا أستاذ silverlight