27-11-17, 06:54 PM
الكود التالي يوضح أحد الأفكار لرسم الجريد ثم و عند اختيارة خلية معينة يتم اعادة تلوينها
الكود يعتبر أساس جيد لتنفيذ فكرتك
انا هنا رسمت الجريد علي الفورم مباشرة
و اعتقد ان عليك استخدام نفس الأسلوب لرسم الجريد علي الكونترول الذي تحدده أنت
الكود يعتبر أساس جيد لتنفيذ فكرتك
انا هنا رسمت الجريد علي الفورم مباشرة
و اعتقد ان عليك استخدام نفس الأسلوب لرسم الجريد علي الكونترول الذي تحدده أنت
PHP كود :
Public Class Form1
Private cellCount As Integer = 100
Private cellPerRow As Integer = 10
Private cellSize As Integer = 20
Private cells As List(Of Rectangle) = Nothing
Private clikedCells As List(Of Rectangle) = New List(Of Rectangle)()
Private selectedCell As Rectangle = Nothing
Private xStart As Integer = 0
Private yStart As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
Me.SetupCells()
End Sub
Private Sub SetupCells()
cells = New List(Of Rectangle)()
Dim indent As Integer = 0
Dim rows As Integer = cellCount / cellPerRow
Dim x As Integer = xStart
Dim y As Integer = yStart
Dim rectSize As Size = New Size(cellSize, cellSize)
For i = 0 To rows - 1
y = xStart
For j As Integer = 1 To cellPerRow
Dim pt As New Point(y, x)
Dim cell As New Rectangle(pt, rectSize)
cells.Add(cell)
y += (cellSize + indent)
Next
x += (cellSize + indent)
Next
End Sub
Private Sub DrawCells(g As Graphics, cellBackColor As Color, cellBorderColor As Color)
For Each cell As Rectangle In cells
g.FillRectangle(New SolidBrush(cellBackColor), cell)
g.DrawRectangle(New Pen(cellBorderColor), cell)
Next
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
DrawCells(e.Graphics, Me.BackColor, Color.Red)
' ----------------------------------
' هذاالسطر من الكود للتوضيح فقط
If Me.selectedCell <> Nothing Then
Dim r As Rectangle = Me.selectedCell
r.X += 2
r.Y += 2
r.Width -= 4
r.Height -= 4
e.Graphics.FillRectangle(New SolidBrush(Color.Blue), r)
End If
' ----------------------------------
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
Me.selectedCell = Me.GetSelectedCell(e.Location)
If selectedCell <> Nothing Then
If Not clikedCells.Contains(selectedCell) Then
Me.clikedCells.Add(selectedCell)
' ----------------------------------
' هذاالسطر من الكود للتوضيح فقط
Me.Text = selectedCell.ToString
Me.Invalidate(selectedCell)
' ----------------------------------
End If
End If
End Sub
Protected Overrides Sub OnSizeChanged(e As EventArgs)
MyBase.OnSizeChanged(e)
Me.SetupCells()
Me.Invalidate()
End Sub
Protected Overrides Sub OnLocationChanged(e As EventArgs)
MyBase.OnLocationChanged(e)
Me.SetupCells()
Me.Invalidate()
End Sub
Private Function GetSelectedCell(pt As Point) As Rectangle
For Each cell As Rectangle In cells
If cell.Contains(pt) Then
Return cell
Exit For
End If
Next
Return Nothing
End Function
End Class
Retired

