Public Class Form1
Public digits As String = "0123456789"
Public userInput As String
Public digitCount As Integer = 0
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
userInput = TextBox1.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
digitCount = userInput.Count(Function(c) Char.IsDigit(c))
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub MaskedTextBox1_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
MaskedTextBox1.Text = digitCount.ToString
End Sub
我运行这个程序。 我在textbox1中输入带有数字的字符串。 单击Button1。 MaskedTextBox1中没有任何内容。 有任何想法吗? 我不知所措。 在我的知识中,我尝试了一切。
答案 0 :(得分:1)
在您的Button1.Click
事件处理程序中,您实际上并没有告诉它做任何事情来设置MaskedTextBox
的值。您似乎放在MaskedTextBox1.MaskInputRejected
处理程序中的那行代码。尝试重构代码,如下所示:
Public Class Form1
Public digits As String = "0123456789"
Public userInput As String
Public digitCount As Integer = 0
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
userInput = TextBox1.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
digitCount = userInput.Count(Function(c) Char.IsDigit(c))
MaskedTextBox1.Text = digitCount.ToString
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub MaskedTextBox1_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
End Sub
答案 1 :(得分:1)
问题在于您没有将数字计数逻辑的输出分配给蒙版文本框,如下所示:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MaskedTextBox1.Text = userInput.Count(Function(c) Char.IsDigit(c)).ToString()
End Sub