2015-12-03 13 views
0

我試圖獲得一個隨機數字來生成並採用所述隨機數字來顯示分配給該數字的某一行文本通過一個Sub標籤程序。使用子過程來調用一個隨機數字並將一個字符串顯示到一個標籤

如果這是任何容易:

  1. 使用Sub過程
  2. 顯示一個字符串,其連接到該隨機數的標籤生成的隨機數1至5
  3. 呼叫隨機數。

我會顯示我的代碼,讓你們知道我的方向是什麼,如果它是正確的。

Public Class Form1 

Private Sub btnInsult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsult.Click 
    Dim strInsult As String 
    Dim intNumber As Integer 
    Randomize() 
    intNumber = Int((5 - 1 + 1) * Rnd() + 1) 

End Sub 
Sub showInsult() 

End Sub 

End Class 

這還真是不多,我想我朝着正確的方向發展。請問我是否需要進一步澄清事情。

回答

0

我有一段類似的代碼用於生成隨機消息。 與上面的代碼不同,這是寫在表單模塊中,而不是一個類,並打印到文本框,而不是標籤。我不知道是否受

顯示的字符串標籤

你實際上意味着改變一個實際的標籤標題。如果是,則使用showInsultEx子。在這裏,它適應您的需求。我希望這有幫助。

Private arrInsults() As Variant 
Private nInsultCount As Integer 

Private Sub Insult_InitRepertoire() 
    'init the insult array 
    arrInsults = Array(_ 
    "Insult 1", _ 
    "Insult 2", _ 
    "Insult 3", _ 
    "Insult 4", _ 
    "Insult 5") 

    nInsultCount = UBound(arrInsults) - LBound(arrInsults) + 1 
End Sub 

Private Sub showInsult() 
    'init the insult array if empty 
    If nInsultCount = 0 Then Insult_InitRepertoire 

    'get a random entry from the insult table 
    'and print it in the text field 
    Randomize 
    Me.TextBox1.Text = arrInsults(LBound(arrInsults) + Int(Rnd() * nInsultCount)) 
End Sub 

Private Sub btnInsult_Click() 
    'invoke the pseudo-random inslut generator 
    showInsult 
End Sub 

Private Sub UserForm_Initialize() 
    'prevent user input 
    Me.TextBox1.Locked = True 
End Sub 

Private Sub showInsultEx() 
    Dim nId As Integer 

    'init the insult array if empty 
    If nInsultCount = 0 Then Insult_InitRepertoire 

    'get a random entry from the insult table 
    'and print it in the text field 
    Randomize 
    nId = LBound(arrInsults) + Int(Rnd() * nInsultCount) 

    'get a control associated with the insult number 
    'change its caption to the generated text 
    'you'll have to make sure to number the controls 
    'according to the array indices 
    Me.Controls("Label" & nId).Caption = arrInsults(nId) 
End Sub 
相關問題