摇号程序(修改版)
求VB程序,实现以下功能:1、把一组要摇号的姓名放在一个1.TXT文档中,
2、读取1.TXT文件的姓名,点击按钮,随机排序,并加序号
3、根据TEXT的宽度和TEXT组件的字号大小,大概算出每行可以显示几个名字,然后设定每行显示这么多名字,然后全部1.TXT文件里的名字全部显示出来!
请问大神,这样的话程序怎么写?
[此贴子已经被作者于2023-4-10 11:09编辑过]
[此贴子已经被作者于2023-4-10 11:09编辑过]
Private Sub btnStart_Click() ' 读取1.TXT文件的姓名 Dim strFileName As String Dim arrNames() As String Dim i As Integer strFileName = "1.TXT" ' 1.TXT文件路径 Open strFileName For Input As #1 Do While Not EOF(1) ReDim Preserve arrNames(i) Line Input #1, arrNames(i) i = i + 1 Loop Close #1 ' 随机排序,并加序号 Dim j As Integer, k As Integer, temp As String For j = LBound(arrNames) To UBound(arrNames) k = Int(Rnd() * (UBound(arrNames) - j) + j) temp = arrNames(j) arrNames(j) = arrNames(k) arrNames(k) = temp arrNames(j) = j + 1 & "." & arrNames(j) ' 加序号 Next j ' 显示随机排序后的姓名 Dim intWidth As Integer, intFontSize As Integer intWidth = TextWidth("姓名") + 20 ' 每个TEXT组件的宽度 intFontSize = 12 ' TEXT组件的字号大小 Dim intCols As Integer, intRows As Integer, intIdx As Integer intCols = Int(Me.Width / intWidth) ' 每行可以显示的名字个数 For i = LBound(arrNames) To UBound(arrNames) intIdx = i Mod intCols If intIdx = 0 Then intRows = intRows + 1 With Me.Controls.Add("Forms.TextBox.1", "txtName" & i) .Top = 40 + intRows * (intFontSize + 5) .Left = 20 + intIdx * intWidth .Text = arrNames(i) .Width = intWidth - 10 .Height = intFontSize + 5 .Font.Size = intFontSize End With Next i End Sub