如何使用Visual Basic 2010将文本数组拆分为3个文本框?

时间:2012-04-23 23:54:52

标签: vb.net

到目前为止,我已经在Virtual Basic 2010中创建了一个随机函数:

  1. 将用户在form2中估算的人名称转换为数组(按空格分割)。
  2. 随机化数组
  3. 为每个被估算的名称显示数组后面带有连续数字和句点。
  4. 以下是源代码:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim names() As String
        Dim i As Integer
        Dim j As Integer
        Dim tmp As String
        Dim txt As String
    
        ' Put the names in an array. SaveTitle is all the text saved in Form2
        names = Split(My.Settings.SaveTitle, " ")
    
        ' Randomize the array.
        Randomize()
        For i = LBound(names) To UBound(names) - 1
            ' Pick a random entry.
            j = Int((UBound(names) - i + 1) * Rnd() + i)
    
            ' Swap the names.
            tmp = names(i)
            names(i) = names(j)
            names(j) = tmp
        Next i
    
        ' Display the results.
        For i = LBound(names) To UBound(names)
            txt = txt & vbCrLf & i + 1 & ". " & names(i)
        Next i
        txt = Mid$(txt, Len(vbCrLf) + 1)
    
        RichTextBox1.Text = txt
    End Sub
    

    注意最后一点。我想取变量txt并拆分它。然后我想获取前10个名称并在RichTextBox1中显示它们,接下来的10个名称并在RichTextBox2中显示它们,并在RichTextBox3中显示最后10个名称。

    我该怎么做?

1 个答案:

答案 0 :(得分:1)

尝试这种方式。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim lstNames() As String
        Dim txt1, txt2, txt3 As String

        ' Put the names in an array. SaveTitle is all the text saved in Form2
        lstNames = Split(My.Settings.SaveTitle, " ")

        ' Randomize the array.
        Randomize()

        ' try this 
        For n As Integer = 0 To lstNames.Count - 1
            lstNames(n) = String.Format("{0}{1} ", lstNames(n), Rnd() + n)
        Next

        For n As Integer = 0 To lstNames.Count - 1
            If n < 10 Then
                txt1 += lstNames(n)
            End If

            If n > 9 And n < 20 Then
                txt2 += lstNames(n)
            End If

            If n > 19 And n < 30 Then
                txt2 += lstNames(n)
            End If

        Next
        RichTextBox1.Text = txt1
        RichTextBox2.Text = txt2
        RichTextBox3.Text = txt3

    End Sub