2012-04-23 50 views
0

我到目前爲止已經創建了虛擬化基礎2010年隨機認爲:如何使用Visual Basic 2010將文本數組分割爲3個文本框?

  1. 注意到用戶在估算到窗體2數組人的名字(由空格分割)。
  2. 隨機化陣列
  3. 顯示陣列中連續的數字和句點後面的每個名稱的估算值。

這裏是源代碼:

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個名稱。

我該怎麼做?

+0

並且您是否嘗試重新拆分新陣列並在一個盒子上打印1到10,在另一個盒子上打印1到10等等? – gbianchi 2012-04-24 16:13:47

回答

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 
相關問題