VB寫的隨機點名器程式碼
'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'1、新建一個工程;
'2、在窗體Form1內放入Label1、Timer1、Command1、Command2四個控制元件;
'3、把以下程式碼直接複製到你的工程裡;
'4、在工程所在的資料夾裡新建一個文字檔案“N.txt”,把名字寫入檔案,一行一個名字。
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
''
'《小小程式設計師》
'Ant 2011.6.25深夜有感
'
'程式碼雖簡單,
'為你解憂煩,
'寫碼傷腦筋,
'佔用我一晚;
'每每向錢看,
'錢包總受傷,
'拷完頂一頂,
'就算幫個忙。
'
Option Explicit
Dim Temp As String
Dim NameTxt(5000) As String '儲存從“N.txt”中逐行讀取的內容,如果大於5000條記錄請更改為更大的數字。
Dim i As Long
Dim Lc As Long 'N.txt中的記錄總數(文字行數)
Dim x As Long ' 儲存隨機數
Private Sub Command1_Click()
i = 0
Lc = 0
x = 999999
Open App.Path & "/N.txt" For Input As #1 '數一數“N.txt”中有幾個記錄。
Do While Not EOF(1)
Line Input #1, Temp
Lc = Lc + 1
NameTxt(Lc) = Temp '把記錄寫入陣列。
Loop
Close #1
If Timer1.Enabled = False Then '控制定時器。
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
Command2.Enabled = True
Command1.Enabled = False
End Sub
Private Sub Command2_Click()
Randomize '產生記錄總數以內的隨機數x
x = Int((Lc - 1 + 1) * Rnd + 1)
Command2.Enabled = False
End Sub
Private Sub Form_Load()
Timer1.Interval = 100 '改變數字100可以調整滾動速度。
Timer1.Enabled = False
Command2.Enabled = False
Me.Caption = "隨機點名"
Command1.Caption = "開始滾動"
Command2.Caption = "停"
Label1.Caption = "名單已準備好"
End Sub
Private Sub Timer1_Timer()
i = i + 1
Label1.Caption = i & " " & NameTxt(i) '顯示記錄
If i = x Then '當滾動隨機數x這條記錄時停止
i = 0
Command1.Enabled = True
Timer1.Enabled = False
Else
If i = Lc Then i = 0
End If
End Sub