2011-11-21 29 views
-4

如何从文本框中拆分2位数字,并将第一位数字放入一个标签中,将第二位数字放入另一个标签中被点击?如何从文本框中拆分2位数字并将数字存储在标签中

+1

我很抱歉,但是那并不是”对我来说,这看起来像一个问题,如果是这样,我不理解它。建议您用代码重写它,以便继续并提出正确的问题。 –

+0

如何在Visual Basic代码中编写它,以便在插入文本框时将数字分隔为两个不同的标签 – Noah

回答

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 

现在还没有测试过(直接从大脑到键盘),但是这是基本的想法,治疗次数为字符的字符串。

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 
相关问题