首页 > 电脑

vb2008速成版如何做到按钮随机排列

更新时间2018-03-06 20:02:41

就是比如4个按钮,点了一个,这些按钮中的3个位置会随机重新排列,并且排列的位置是固定的几个,然后可以重复任意次数,且次数要记录下来,求代码

    Dim btns() As Button, Times As Integer
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim I As Integer, BW As Integer, BH As Integer, WW As Integer, WH As Integer, L As Integer, T As Integer
        CenterFormAndSetInnerSize(1280, 720)
        With Me.ClientSize
            WW = .Width
            WH = .Height
        End With
        ReDim btns(0 To 3)
        For I = 0 To 3
            Btns(I) = New Button
            Me.Controls.Add(Btns(I))
            With Btns(I)
                .Text = "按钮" & Trim$(I)
                BW = Int(Rnd() * 64) + 32
                BH = Int(Rnd() * 32) + 32
                L = WW * Rnd() : T = WH * Rnd()
                If L + BW + 1 > WW Then L = WW - BW - 1
                If T + BH + 1 > WH Then T = WH - BH - 1
                .SetBounds(L, T, BW, BH)
                .Visible = True
            End With
            AddHandler Btns(I).Click, AddressOf Button1_Click
        Next
        Me.Text = "按照固定位置随机排列的 0 次"
    End Sub
    Private Sub CenterFormAndSetInnerSize(ByVal WidthPixel As Integer, ByVal HeightPixel As Integer)
        Dim W, Dx, Dy, H As Integer
        With Me
            Dx = .Width - .ClientSize.Width
            Dy = .Height - .ClientSize.Height
            W = WidthPixel + Dx : H = HeightPixel + Dy
        End With
        With System.Windows.Forms.Screen.FromControl(Me)
            Me.SetBounds((.Bounds.Width - W)  2, (.Bounds.Height - H)  2, W, H)
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim I As Integer, U As Integer, Pts() As System.Drawing.Point, T As System.Drawing.Point, A As Integer, B As Integer
        U = Btns.Length - 1
        ReDim Pts(0 To U)
        For I = 0 To U
            Pts(I) = Btns(I).Location
        Next
        For I = 1 To 100
            A = Int(Rnd() * 4)
            Do : B = Int(Rnd() * 4) : Loop Until A <> B
            T = Pts(A)
            Pts(A) = Pts(B)
            Pts(B) = T
        Next
        For I = 0 To U
            Btns(I).Location = Pts(I)
        Next
        Times = Times + 1
        Me.Text = "按照固定位置随机排列的 " & Trim$(Times) & " 次"
    End Sub

 

上一篇:qqqqqqq问题的

下一篇:VB,看下面这段代码,把文本框换成列表框该怎么改?