Private m_double As pic '记录相同的那个卡片
Private m_last As Integer '记录格子中的最后一个卡片
'开始排列
Public Sub start()
Me.Controls.Clear() '先清空容器
If Not IsNothing(m_collection) Then '判断行列之积和卡片数量是否相等
If (m_collection.count <> m_rows * m_columns) Then
Throw New Exception("卡片数量为:" & CStr(m_collection.count) & "格子数量为:" & CStr(m_rows * m_columns))
End If
'///////////////////////////////////
m_last = -2 '初始化,因为从0开始是第一个格子,所以初始值为-2
m_collection.setfalse() '因为开始一次排序就会把所有的卡片pic的double属性全都设置为true。所以,这里要全都设置回false
m_collection.shuffle() '将卡片弄混
Dim cardcount As Integer = 0 '卡片指针
Dim row, column As Integer
For row = 0 To m_rows - 1
For column = 0 To m_columns - 1
Dim apic As pic = CType(m_collection(cardcount), pic)
'加入到me的控件集合
Me.Controls.Add(apic)
'控件集合中的原有卡片进行遍历,看是否有单个的与新加入的同一个图片的卡片
Dim rint As Integer = contrains(apic)
Select Case rint
Case 0 '匹配排列
apic.Left = m_double.Left + m_spacing + my_namespace.pic.m_maxlen
apic.Top = m_double.Top
Case 1 '没有匹配项,间隔排列
'行,注意一定要使用int进行转化,否则会四舍五入。
Dim r As Integer = Int(m_last / (m_rows))
'列
Dim c As Integer = m_last Mod (m_rows)
'取得行列后就可以直接设置位置了。
apic.Left = c * (pic.m_maxlen + m_spacing) + m_spacing
apic.Top = r * (pic.m_maxheight + m_spacing) + m_spacing
End Select
cardcount += 1 '下一个卡片
Next
Next
MessageBox.Show("排序完成!")
End If
End Sub
'排序的函数
Public Function contrains(ByVal p As pic) As Integer
m_double = Nothing '初始值为空,每次排序前要设置为空。
Dim apic As pic
Dim i As Integer
Dim count As Integer = Me.Controls.Count - 1
For i = 0 To count - 1 '从0到末尾-1,把自己排除掉,自己和自己不必要去比较
apic = CType(Me.Controls(i), pic)
If (apic.id = p.id) And apic.doubles = False Then 'And i <> count
apic.doubles = True '匹配
p.doubles = True '匹配
m_double = apic