1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
Public Class Form1
Private WB As Integer = 100 'largeur button
Private HB As Integer = 40 'hauteur button
Private FCols As Integer = 4 'nb colonnes
Private FRows As Integer = 4 'nb lignes
Private SepX As Integer = 10 'espacement Hor
Private SepY As Integer = 10 'espacement Ver
Private X As Integer
Private Y As Integer
Private Numeros(15) As Integer 'tableau numeros
Private GridButtons(FRows - 1, FCols - 1) As Button 'futile, peut etre eliminé
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.Panel1.BorderStyle = BorderStyle.Fixed3D
Me.Panel1.BackColor = Color.White
Me.Size = New Size(FCols * (WB + SepX), FRows * (HB + SepY))
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
GenNumeros()
CreateBoard()
End Sub
'reset du pave grid
Private Sub ResetButton_Click_1(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
GenNumeros()
CreateBoard()
End Sub
'tirage au sort des numeros :0...15
Private Sub GenNumeros()
Array.Clear(Numeros, 0, Numeros.Length)
Numeros = GetRandomNumbers(Numeros.Length)
End Sub
'dessin du board
Private Sub CreateBoard()
'clear tout y compris le Panel1 "hote"
Array.Clear(GridButtons, 0, 16)
Me.Panel1.Controls.Clear()
Dim index As Integer = 0
For col As Integer = 0 To FCols - 1
For row As Integer = 0 To FRows - 1
X = row * (WB + SepX)
Y = col * (HB + SepY)
Dim btn As New Button()
Me.Panel1.Controls.Add(btn)
index = Me.Panel1.Controls.IndexOf(btn)
'curseur Croix
btn.Cursor = Cursors.SizeAll
btn.Location = New Point(X, Y)
btn.Size = New Size(WB, HB)
btn.Font = Me.Font
btn.Name = "btn" + Numeros(index).ToString()
btn.Text = Numeros(index).ToString()
'allow drop
btn.AllowDrop = True
If Numeros(index) = 0 Then
btn.BackColor = Color.DarkBlue
Else
btn.BackColor = Color.DarkGreen
End If
AddHandler btn.MouseMove, AddressOf btn_mousemove
AddHandler btn.DragEnter, AddressOf btn_dragenter
AddHandler btn.DragDrop, AddressOf btn_dragdrop
GridButtons(row, col) = btn
Next
Next
End Sub
Private Sub btn_mousemove(sender As Object, e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
Dim btn As Button = CType(sender, Button)
' sender could be btnA or btnB - we dont care
btn.DoDragDrop(btn, DragDropEffects.Copy)
End If
End Sub
Private Sub btn_dragenter(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent(GetType(Button)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub btn_dragdrop(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent(GetType(Button)) = False Then
Exit Sub
End If
' sender == bouton CIBLE
Dim btnDst As Button = CType(sender, Button)
Dim posDst As Point = btnDst.Location
' e.Data == bouton SOURCE
Dim bSrc As Button = CType(e.Data.GetData(GetType(Button)), Button)
Dim posSrc As Point = bSrc.Location
'
Dim pt As Point
If (posDst.X = posSrc.X And posDst.Y = posSrc.Y + SepY + HB) Or
(posDst.X = posSrc.X And posDst.Y = posSrc.Y - SepY - HB) Then 'bouton CIBLE est en HAUT ou BAS
pt = btnDst.Location
' swap locations
btnDst.Location = bSrc.Location
bSrc.Location = pt
ElseIf (posDst.X = posSrc.X + SepX + WB And posDst.Y = posSrc.Y) Or
(posDst.X = posSrc.X - SepX - WB And posDst.Y = posSrc.Y) Then 'bouton CIBLE est à DROITE ou à GAUCHE
pt = btnDst.Location
' swap locations
btnDst.Location = bSrc.Location
bSrc.Location = pt
End If
End Sub
'tirage au sort
Private rand As New Random()
Private Function GetRandomNumbers(n As Integer) As Integer()
Dim dstList As New List(Of Integer)
While dstList.Count < n
Dim number As Integer = rand.Next(0, n)
If Not dstList.Contains(number) Then
dstList.Add(number)
End If
End While
Return dstList.ToArray()
End Function
End Class |
Partager