如何点击文本框中的2位数字,并在点击按钮时将第一个数字放在一个标签中,将第二个数字放在另一个标签中?
答案 0 :(得分:1)
这假设表单上有一个文本框(TextBox1),两个标签(Label1,Label2)和一个按钮(Button1)。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "First digit:" & TextBox1.Text.Substring(0, 1)
Label2.Text = "Second digit:" & TextBox1.Text.Substring(1, 1)
End Sub
当然你应该测试长度,如果它真的是一个数字输入之前,所以添加一些检查将是一个很好的做法:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IsNumeric(TextBox1.Text) Then
MsgBox("You have to enter a number between 10 and 99 in the textbox.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly, "Not a number")
ElseIf TextBox1.Text.Length <> 2 Then
MsgBox("You have to enter a 2 digit number in the textbox.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly, "Not a 2 digit number")
Else
Label1.Text = "First digit:" & TextBox1.Text.Substring(0, 1)
Label2.Text = "Second digit:" & TextBox1.Text.Substring(1, 1)
End If
End Sub
答案 1 :(得分:0)
假设您要在文本框中键入数字,请单击按钮,然后让文本框显示数字,数字用空格分隔。
Dim sNum As String
sNum = tbNum.Value 'assuming tbNum is the name of the textbox control
Dim sNewNum As String
Dim i As Integer
For i = 1 To Len( sNum )
sNewNum = Mid( sNum, i, 1 )
If i < Len( sNum ) Then sNewNum = sNewNum & " "
Next
tbNum.Value = sNewNum
现在还没有经过测试(直接从大脑到键盘),但这是基本想法,将数字视为一串字符。