none
我想请问各位我可已什么方式来detect 到我的textbox 输入的数字还剩下我所设定的多少。 RRS feed

  • 问题

  • 我想请问各位我可已什么方式来detect 到我的textbox 输入的数字还剩下我所设定的多少。我目前是已msg box 方式呈现

       txtOlocation.MaxLength = 10
            If Trim(txtOlocation.Text).Length = 10 Then
                MessageBox.Show("Maximun is 10 Word", "Error Message")
            End If


    2019年12月11日 9:57

答案

  • Hi christing, 

    根据你的问题,我创建了一个继承自TextBox的自定义控件,并向其中添加了两个Label,它基本上实现了相似的效果.

    代码如下:

    我的自定义控件:

    Public Class PlaceHolderTextBox
        Inherits TextBox
        Private isPlaceHolder = True
        Private _placeHolderText As String
    
        Public Property PlaceHolderText As String
            Get
                Return _placeHolderText
            End Get
            Set(ByVal value As String)
                _placeHolderText = value
                setPlaceholder()
            End Set
        End Property
    
        Public Overloads Property Text As String
            Get
                Return If(isPlaceHolder, String.Empty, MyBase.Text)
            End Get
            Set(ByVal value As String)
                MyBase.Text = value
            End Set
        End Property
    
        Private Sub setPlaceholder()
            If String.IsNullOrEmpty(MyBase.Text) Then
                MyBase.Text = PlaceHolderText
                Me.ForeColor = Color.Gray
                Me.Font = New Font(Me.Font, FontStyle.Italic)
                isPlaceHolder = True
            End If
        End Sub
    
        Private Sub removePlaceHolder()
            If isPlaceHolder Then
                MyBase.Text = ""
                Me.ForeColor = System.Drawing.SystemColors.WindowText
                Me.Font = New Font(Me.Font, FontStyle.Regular)
                isPlaceHolder = False
            End If
        End Sub
    
        Public Sub New()
            AddHandler GotFocus, AddressOf removePlaceHolder
            AddHandler LostFocus, AddressOf setPlaceholder
        End Sub
    
        Private Sub setPlaceholder(ByVal sender As Object, ByVal e As EventArgs)
            setPlaceholder()
        End Sub
    
        Private Sub removePlaceHolder(ByVal sender As Object, ByVal e As EventArgs)
            removePlaceHolder()
        End Sub
    End Class

    Build项目以后你可以在Toolbox中看到该控件:

    创建该控件,并设置其PlaceHolderText为‘输入(最多十个)’,

    随后我创建了两个Label, 调整好位置并设置初始值.

    设置TextBoxTextChanged事件,代码如下:

        Private Sub PlaceHolderTextBox1_TextChanged(sender As Object, e As EventArgs) Handles PlaceHolderTextBox1.TextChanged
            PlaceHolderTextBox1.MaxLength = 10
            Dim number As Integer = PlaceHolderTextBox1.Text.Count
            Label1.Text = number.ToString
    
            If number = 10 Then
                MessageBox.Show("输入内容已达上限!")
            End If
        End Sub
    

    运行结果:

    希望这对你有帮助.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.






    2019年12月12日 6:52
    版主

全部回复

  • Hi christing, 

    根据你的问题,我创建了一个继承自TextBox的自定义控件,并向其中添加了两个Label,它基本上实现了相似的效果.

    代码如下:

    我的自定义控件:

    Public Class PlaceHolderTextBox
        Inherits TextBox
        Private isPlaceHolder = True
        Private _placeHolderText As String
    
        Public Property PlaceHolderText As String
            Get
                Return _placeHolderText
            End Get
            Set(ByVal value As String)
                _placeHolderText = value
                setPlaceholder()
            End Set
        End Property
    
        Public Overloads Property Text As String
            Get
                Return If(isPlaceHolder, String.Empty, MyBase.Text)
            End Get
            Set(ByVal value As String)
                MyBase.Text = value
            End Set
        End Property
    
        Private Sub setPlaceholder()
            If String.IsNullOrEmpty(MyBase.Text) Then
                MyBase.Text = PlaceHolderText
                Me.ForeColor = Color.Gray
                Me.Font = New Font(Me.Font, FontStyle.Italic)
                isPlaceHolder = True
            End If
        End Sub
    
        Private Sub removePlaceHolder()
            If isPlaceHolder Then
                MyBase.Text = ""
                Me.ForeColor = System.Drawing.SystemColors.WindowText
                Me.Font = New Font(Me.Font, FontStyle.Regular)
                isPlaceHolder = False
            End If
        End Sub
    
        Public Sub New()
            AddHandler GotFocus, AddressOf removePlaceHolder
            AddHandler LostFocus, AddressOf setPlaceholder
        End Sub
    
        Private Sub setPlaceholder(ByVal sender As Object, ByVal e As EventArgs)
            setPlaceholder()
        End Sub
    
        Private Sub removePlaceHolder(ByVal sender As Object, ByVal e As EventArgs)
            removePlaceHolder()
        End Sub
    End Class

    Build项目以后你可以在Toolbox中看到该控件:

    创建该控件,并设置其PlaceHolderText为‘输入(最多十个)’,

    随后我创建了两个Label, 调整好位置并设置初始值.

    设置TextBoxTextChanged事件,代码如下:

        Private Sub PlaceHolderTextBox1_TextChanged(sender As Object, e As EventArgs) Handles PlaceHolderTextBox1.TextChanged
            PlaceHolderTextBox1.MaxLength = 10
            Dim number As Integer = PlaceHolderTextBox1.Text.Count
            Label1.Text = number.ToString
    
            If number = 10 Then
                MessageBox.Show("输入内容已达上限!")
            End If
        End Sub
    

    运行结果:

    希望这对你有帮助.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.






    2019年12月12日 6:52
    版主
  • @Xingyu Zhao

    你好厉害丫。谢谢你的答复与答案


    2019年12月12日 9:32