VB製作的樂透抽獎機
沒有很美觀就是了
還存在一些BUG!
這是Form1的圖
這是Form2的圖
Form1程式碼
Public Class Form1 Dim form2 As Form2 Dim ball(48) As PictureBox Dim tmpInt(48), tmpInt2(48) As Integer Dim reward(5) As String Dim j As Integer = 0 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Application.Exit() End Sub Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click form2 = New Form2() form2.ShowDialog() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Randomize() Me.createBall() PictureBox1.BackColor = Color.Gray Me.FormBorderStyle = 1 End Sub Private Sub btnDirStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirStart.Click Timer1.Enabled = True Timer2.Enabled = True End Sub Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click If TextBox1.Text = "" Then MessageBox.Show("您沒選擇號碼") Else TextBox1.Text = "" Array.Clear(form2.choNumber, 0, 6) End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Me.moveBall() End Sub Private Sub createBall() Dim pointx As Integer Dim pointy As Integer For i = 0 To 48 pointx = ranNumberX() pointy = ranNumberY() ball(i) = New PictureBox ball(i).Name = (i + 1).ToString() ball(i).Location = New System.Drawing.Point(pointx, pointy) ball(i).Size = New System.Drawing.Size(30, 30) ball(i).Image = Image.FromFile("balls\ball" & i + 1 & ".png") PictureBox1.Controls.Add(ball(i)) Next End Sub Function ranNumberY() As Integer Return CInt(Rnd() * 300 + 1) End Function Function ranNumberX() As Integer Return CInt(Rnd() * 500 + 1) End Function Private Sub moveBall() Dim ranA As Integer For i = 0 To 48 ranA = CInt(Rnd() * 20 + 10) Select Case tmpInt(i) Mod 2 Case 0 If ball(i).Left < PictureBox1.Width - ball(i).Width Then ball(i).Left = ball(i).Left + ranA Else tmpInt(i) = tmpInt(i) + 1 End If Case 1 If ball(i).Left > 0 Then ball(i).Left = ball(i).Left - ranA Else tmpInt(i) = tmpInt(i) - 1 End If End Select Select Case tmpInt2(i) Mod 2 Case 0 If ball(i).Top < PictureBox1.Height - ball(i).Height Then ball(i).Top = ball(i).Top + ranA Else tmpInt2(i) = tmpInt2(i) + 1 End If Case 1 If ball(i).Top > 0 Then ball(i).Top = ball(i).Top - ranA Else tmpInt2(i) = tmpInt2(i) - 1 End If End Select If ball(i).Left >= 340 And ball(i).Left <= 350 Then If ball(i).Top >= 0 And ball(i).Top <= 5 Then TextBox2.Text += ball(i).Name + " " reward(j) = ball(i).Name PictureBox1.Controls.Remove(ball(i)) j = j + 1 End If End If Next End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub Timer2_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick If reward(5) <> 0 Then Timer1.Enabled = False If TextBox1.Text.Equals(TextBox2.Text) Then Label3.Text = "恭喜中獎" Else Label3.Text = "沒中啦!!" End If End If End Sub Private Sub Label3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.TextChanged Timer2.Enabled = False End Sub End Class
Form2程式碼
Public Class Form2 Public choNumber(5) As Integer Private Sub btnChk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChk.Click Dim j As Integer = 0 For i = 0 To CheckedListBox1.Items.Count - 1 If CheckedListBox1.GetItemChecked(i) Then TextBox1.Text += CheckedListBox1.Items(i).ToString() + " " choNumber(j) = CheckedListBox1.Items(i) j = j + 1 End If Next End Sub Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged Dim count As Integer = 0 For i = 0 To CheckedListBox1.Items.Count - 1 If CheckedListBox1.GetItemChecked(i) Then count = count + 1 If count > 6 Then MessageBox.Show("只能選6個唷") End If End If Next End Sub Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click Me.Close() Array.Clear(choNumber, 0, 6) End Sub Private Sub btnResetN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetN.Click For i = 0 To CheckedListBox1.Items.Count - 1 CheckedListBox1.SetItemChecked(i, False) Next TextBox1.Text = "" Array.Clear(choNumber, 0, 6) End Sub Private Sub btnBegan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBegan.Click If TextBox1.Text = "" Then MessageBox.Show("選完號碼後請按左邊選號") MessageBox.Show("若要關閉此視窗請案回主畫面") Exit Sub End If Form1.TextBox1.Text = TextBox1.Text Form1.Timer1.Enabled = True Form1.Timer2.Enabled = True Me.Close() End Sub End Class
全站熱搜