想请教字数多过自己max字数后多余的字数怎么篇写进另一个label RRS feed

  • 问题

  • 想请教如何篇多余的字数

    我篇写了一个当textbox1 过了字数后会出现error message的code。

    我想篇写如果textbox1 过多字数后会在label 呈现出过多了多少字数的code

    TextBox1.MaxLength = 15
            Dim count As Integer
            Dim number As Integer = TextBox1.Text.Count

            If number = 15 Then
                MessageBox.Show("Maximun is 15 Word", "Error Message")
            End If

    比如textbox1 到了15字数会出现(MessageBox.Show("Maximun is 15 Word", "Error Message"))

    当textbox1 在输入多一些三个字后

    textbox 总共出现18 个字 label 会呈现3

    2020年3月3日 3:06


  • Hi christing,

    根据你的描述,你需要 Control.TextChanged 事件来实时监测你 TextBox 中的值.


        Private Const maxLength As Integer = 15
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            If TextBox1.Text.Length = maxLength Then
                MessageBox.Show("Maximun is 15 Word", "Error Message")
            End If
            If TextBox1.Text.Length > maxLength Then
                Label1.Text = TextBox1.Text.Length - maxLength & ""
            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

    • 已标记为答案 christing 2020年3月4日 1:54
    2020年3月3日 6:23