Public Sub shuffle() ' 混卡片,也就是生成一组随机的卡片集合。(这里的算法不错!)
Dim r As New System.Random()
Dim d As New System.Collections.ArrayList()
Dim p As pic
While (m_piccontrols.Count > 0)
Dim removeindex As Integer = r.Next(0, m_piccontrols.Count - 1)
p = CType(m_piccontrols(removeindex), my_namespace.pic)
m_piccontrols.RemoveAt(removeindex)
d.Add(p)
End While
m_piccontrols = d
End Sub
Private m_image As imagelist
<Category("grid"), Description("选择相应的imagelist控件。")> _
Public Property imagelist() As imagelist
Get
Return m_image
End Get
Set(ByVal Value As imagelist)
m_image = Value
changepic()
End Set
End Property
'/////////
'这个事件比较重要,主要是根据图片的变动来生成不同的卡片集合。
Private Sub changepic()
If m_image Is Nothing Then Exit Sub
Dim i As Integer
For i = 0 To m_piccontrols.Count - 1
CType(m_piccontrols(i), pic).Dispose() '注意这里。
Next
m_piccontrols.Clear()
Dim j As Integer
For i = 0 To m_image.Images.Count - 1
For j = 0 To 3
Dim p As New pic(m_image.Images(i))
p.id = i
m_piccontrols.Add(p)
Next
Next
End Sub
'由于在排列好后,每个在集合中的卡片的doubles属性都会被设置成true,
'所以要在开始一次新的排序时设置所有的卡片该属性为false
Public Sub setfalse()
Dim i As Integer
For i = 0 To m_piccontrols.Count - 1
Dim apic As pic = CType(m_piccontrols(i), pic)
apic.doubles = False
Next
End Sub
End Class
3.
Imports System.ComponentModel
Public Class picshow
Inherits System.Windows.Forms.UserControl
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
'// 这个程序的原理 //
'//先做一个pic控件,可以为其设置相应的图片,不允许改变大小,要重写sizechange,onpait事件 //
'//做一个集合组件piccontrols来容纳一定数量的pic卡片,但并不显示它,因为是组件。只是容器 //
'//最后做一个picshow控件,用于显示piccontrols.count数量的卡片集合。 //
'//比较重要的地方就是如何对卡片进行随机混排(piccontrols的shuffle方法)和picshow控件的 //
'//contrains,start方法。尤其注意这里进行排序的方法:是将卡片在集合里就弄混(随机),这样//
'//我们取得的每个卡片都是随机的了,然后在picshow控件里根据每个卡片的doubles,id属性来进行 //
'//排序,把随机和排序分开了。当然也可以把他们合并写到picshow控件里。不过这里不建议这样。 //
'//因为对于piccontrols组件来说,它的集合就是一个随机产生的卡片集合。这样比较好理解。 //
Private Const m_spacing As Integer = 10 '间隔设置的常量
Private m_rows As Integer = 2 ' 对于一个阵列来讲,2行应该更有意义。
<Category("grid"), Description("矩阵的行。"), DefaultValue(2)> _
Public Property row() As Integer
Get
Return m_rows
End Get
Set(ByVal Value As Integer)
If Value > 0 Then
m_rows = Value
Me.Refresh()
End If
End Set
End Property
Private m_columns As Integer = 2
<Category("grid"), Description("矩阵的列。"), DefaultValue(2)> _
Public Property columns() As Integer
Get
Return m_columns
End Get
Set(ByVal Value As Integer)