Pictureboxgrid anyone?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I was working on this for giggles... Thought someone might like it...
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/137745
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/137759
<pre class="prettyprint lang-vb" style=" Option Strict On
Imports System.Text
Public Class PictureBoxGrid
Inherits UserControl
Public Const MaxColumns As Integer = 100
Public Const MinColumns As Integer = 1
Public Const MaxRows As Integer = 100
Public Const MinRows As Integer = 1
Public Const MaxCellWidth As Integer = 200
Public Const MaxCellHeight As Integer = 200
Public Const MinCellWidth As Integer = 1
Public Const MinCellHeight As Integer = 1
Private Property _ColumnCount As Integer = 10
Private Property _RowCount As Integer = 10
Private Property _CellSize As New Size(10, 10)
Private Property _SeperationSize As Integer = 1
Public Property RandomCellColors As Boolean = True
Public Property Seperationsize As Integer
Get
Return _SeperationSize
End Get
Set(value As Integer)
_SeperationSize = value
DrawGrid()
UpdateSize()
End Set
End Property

Private Property _MouseCooridinate As New Point(0, 0)
Public Event CoordinateChanged(Coordinate As Point)
Public ReadOnly Property MouseCooridinate As Point
Get
Return _MouseCooridinate
End Get
End Property
Public Property Columncount As Integer
Get
Return _ColumnCount
End Get
Set(value As Integer)
If Not value > MaxColumns And Not value < MinColumns Then
_ColumnCount = value
DrawGrid()
UpdateSize()
Else
Throw New ArgumentOutOfRangeException("ColumnCount", "Value Must Be Between " & MinColumns.ToString & " and " & MaxColumns.ToString & ".")
End If
End Set
End Property
Public Property RowCount As Integer
Get
Return _RowCount
End Get
Set(value As Integer)
If Not value > MaxRows And Not value < MinRows Then
_RowCount = value
DrawGrid()
UpdateSize()
Else
Throw New ArgumentOutOfRangeException("RowCount", "Value Must Be Between " & MinRows.ToString & " and " & MaxRows.ToString & ".")
End If
End Set
End Property
Public Property CellSize As Size
Get
Return _CellSize
End Get
Set(value As Size)
If Not value.Width > MaxCellWidth And _
Not value.Width < MinCellWidth And _
Not value.Height > MaxCellHeight And _
Not value.Height < MinCellHeight Then
_CellSize = value
DrawGrid()
UpdateSize()
Else
Dim ErrorList As New List(Of String)
If value.Width > MaxCellWidth Then ErrorList.Add("CellSize.Width out of range(" & MinCellWidth.ToString & "-" & MaxCellWidth.ToString & ")")
If value.Width < MinCellWidth Then ErrorList.Add("CellSize.Width out of range(" & MinCellWidth.ToString & "-" & MaxCellWidth.ToString & ")")
If value.Height > MaxCellHeight Then ErrorList.Add("CellSize.Height out of range(" & MinCellHeight.ToString & "-" & MaxCellHeight.ToString & ")")
If value.Height < MinCellHeight Then ErrorList.Add("CellSize.Height out of range(" & MinCellHeight.ToString & "-" & MaxCellHeight.ToString & ")")
Dim ErrorMessage As New StringBuilder
ErrorMessage.Append("The following errors occurred:" & vbCrLf)
For Each Err As String In ErrorList
ErrorMessage.Append(Err & vbCrLf)
Next
Throw New ArgumentOutOfRangeException("CellSize", ErrorMessage.ToString)
End If
End Set
End Property
Public Sub SetCellColor(CellCoordinate As Point, Color As Color)
Dim FindTag As String = CellCoordinate.X.ToString & "|" & CellCoordinate.Y.ToString
Dim PBType As Type = GetType(PictureBox)
For Each C As Control In Me.Controls
If C.GetType = PBType Then
Dim PB As PictureBox = DirectCast(C, PictureBox)
If FindTag = PB.Tag.ToString Then
PB.BackColor = Color
Exit For
End If
End If
Next
End Sub

Private Sub PictureBoxGrid_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Public Sub UpdateSize()
Me.Width = (CellSize.Width * _ColumnCount) + (_ColumnCount * SeperationSize) - SeperationSize
Me.Height = (CellSize.Height * _RowCount) + (_RowCount * SeperationSize) - SeperationSize
Me.MaximumSize = New Size((CellSize.Width * _ColumnCount) + (_ColumnCount * SeperationSize) - SeperationSize, _
(CellSize.Height * _RowCount) + (_RowCount * SeperationSize) - SeperationSize)
Me.MinimumSize = New Size((CellSize.Width * _ColumnCount) + (_ColumnCount * SeperationSize) - SeperationSize, _
(CellSize.Height * _RowCount) + (_RowCount * SeperationSize) - SeperationSize)
End Sub
Sub New()
This call is required by the designer.
InitializeComponent()
Add any initialization after the InitializeComponent() call.
DrawGrid()
Me.UpdateSize()
End Sub
Public Sub DrawGrid()
Me.Controls.Clear()
Dim AddQuantity As Integer = Columncount * RowCount - 1
Dim Left As Integer = 0
Dim Top As Integer = 0
Dim ControlNumber As Integer = 0
Dim X As Integer = 1
Dim Y As Integer = 1
For I = 0 To AddQuantity
ControlNumber = ControlNumber + 1
Dim PB As New PictureBox
With PB
.Width = CellSize.Width
.Height = CellSize.Height
.Left = Left
.Top = Top
.Name = "Cell" & ControlNumber.ToString
.BorderStyle = Windows.Forms.BorderStyle.None
Select Case RandomCellColors
Case True
.BackColor = RandomColor()
Case Else
.BackColor = Color.White
End Select


.Tag = X.ToString & "|" & Y.ToString
End With
Me.BackColor = RandomColor()
AddHandler PB.Click, AddressOf clickPB
AddHandler PB.MouseMove, AddressOf mouseMovePB
Left = Left + PB.Width + SeperationSize
If ControlNumber Mod Columncount = 0 Then
Y = 0
X = X + 1
Left = 0
Top = Top + CellSize.Height + SeperationSize
End If
Me.Controls.Add(PB)
Y = Y + 1
Next
UpdateSize()
End Sub
Sub mouseMovePB(sender As Object, e As MouseEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
Dim X As Integer
Dim Y As Integer
Dim Coordinates() As String = CType(PB.Tag, String).Split(CChar("|"))
X = CInt(Coordinates(0))
Y = CInt(Coordinates(1))
RaiseEvent CoordinateChanged(New Point(X, Y))
End Sub
Sub clickPB(sender As Object, e As System.EventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
MsgBox(PB.Tag)
End Sub
Function RandomColor() As Color
Dim Alpha As Integer
Dim Red As Integer
Dim Green As Integer
Dim Blue As Integer
Randomize()
Alpha = 255 CInt(Int((254 * Rnd()) + 0))
Randomize()
Red = CInt(Int((254 * Rnd()) + 0))
Randomize()
Green = CInt(Int((254 * Rnd()) + 0))
Randomize()
Blue = CInt(Int((254 * Rnd()) + 0))
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()

PictureBoxGrid

Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
Me.DoubleBuffered = True
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Name = "PictureBoxGrid"
Me.Size = New System.Drawing.Size(337, 308)
Me.ResumeLayout(False)
End Sub
End Class[/code]
<br/>

<
If you want something youve never had, you need to do something youve never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
<br/>

View the full article
 
Back
Top