Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / Visual Basic
Article

Graphical grid component

Rate me:
Please Sign up or sign in to vote.
4.70/5 (21 votes)
13 Jan 20044 min read 172.7K   3.2K   41   40
Easy manipulation of graphical grids

Sample Image - picture.png

Introduction

GraphicGrid is a control for your .NET applications that makes graphical grid manipulation easier. GraphicalGrids can be used for a wide range of tasks including graphing applications and games just to name a few.

GraphicGrid divides up its workspace and allows you to control what you want to place in the individual cells of your grid. You can populate some or all of them with pictures, colors or even graphical text while leaving others blank. GraphicGrid also allows you to load an image on to the background of its workspace so you can create really interesting and eye catching effects.

This Distribution

I have included five files for you to download:

  • GraphicGrid_DLL.zip
    This is the source code and the compiled GraphicGrid DLL.
  • GraphicGrid_Game.zip
    GraphicGrid_GameSrc.zip
    A very simple game where you wack (click on) the rabbits that appear on the screen. Very similar to the caravel game "Wack a Mole".
  • GraphicGrid_Sample.zip
    GraphicGrid_SampleSrc.zip
    A sample to demonstrate how to use graphicGrid.

Getting GraphicGrid into your toolbox

I have not made an installed or a cool icon for the control yet so this is what you have to do.

  1. Extract GraphicGrid_DLL.zip to a directory under Visual Studio Projects.
  2. Create a new project and go to design view.
  3. Look over at your toolbox and right click on the name of the tab where you want to put GraphicGrid and select "Add/Remove Items..."
  4. Click on the browse button and select GraphicGrid.DLL from the directory where you extracted it.
  5. Click "OK" and you will now have the GraphicGrid tool in your toolbox.

Special Properties

ShowGridThis is true or false if you want the grid to display in the controls workspace
Example: object.ShowGrid = True
GridColorWhat color you want the grid to be
Example: object.GridColor = color.red
CellsHow many grids you want (row x col)
Example: object.Cells = new size(10, 10)

Special Methods

setCell

Definition: setCell(Cell As Point, newColor As Color)
Example: object.setCell(New Point(9, 15), Color.White)
Note: This sets any one cell to be filled in with a color

Definition: setCell(Cells As Point(), newColor As Color)
Example: object.setCell({New Point(9, 15), New Point(9,16)}, Color.White) <BR>Note: This sets a range of cells to be filled in with a color

Definition: setCell(Cell As Point, newBitmap As Bitmap)<BR>Example: object.setCell(New Point(9, 15), new bitmap("cell.png"))<BR>Note: This sets a cell to be filled with a bitmap

Definition: setCell(ByVal Cells As Point(), ByVal newBitmap As Bitmap)<BR>Example: object.setCell({New Point(9, 15), New Point(9,16)}, new bitmap("cell.png")) <BR>Note: This sets a range of cells to be filled in with a bitmap

removeCell

Definition: removeCell(Cell as Point) <BR>Example: object.removeCell(New Point(9, 15)) <BR>Note: This removes a cell from the grid

Definition: removeCell(Cells As Point())<BR>Example: object.removeCell({New Point(9, 15), New Point(9,16)}) <BR>Note: This removes a range of cells from the grid

clearCellsDefinition: clearCells()<BR>Example: object.clearCells()<BR>Note: This removes all cells from the grid

Special Events

gridClickThis is fired when you click somewhere in the grid.
Returns: Object and GridPosition
gridDoubleClickThis is fired when you double click somewhere in the grid.
Returns: Object and GridPosition
gridMouseMoveThis is fired when you move the mouse somewhere in the grid.
Returns: Object and GridPosition
gridMouseUpThis is fired after you click somewhere in the grid.
Returns: Object, MouseEventArgs and GridPosition
gridMouseDownThis is fired after you click somewhere in the grid.
Returns: Object, MouseEventArgs and GridPosition

GridPosition is always expressed as a Point. Lets say your grid was 10x10 and the user clicks in the 5th cell of the 5th row. In any of these special events the value of gridPosition would be point(5, 5).

Detailed comments can also be found in the DLL source files.

Sample Application

Sample image

We start by draging the panel control and the GraphicGrid control on to the form and setting them to dock. Add a few buttons in the panel and here's what we have them do.

VB.NET
  Private Sub allButtons(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles Button1.Click, Button2.Click, Button3.Click, _
            Button4.Click, Button5.Click, Button6.Click


      ' This is just a big array of points
      ' (Used in create onscreen cells)
      '
      Dim points() As Point = { _
New Point(0, 0), New Point(0, 1), New Point(0, 2), New Point(0, 3), _
New Point(1, 0), New Point(2, 1), New Point(3, 0), _
New Point(4, 0), New Point(4, 1), New Point(4, 2), New Point(4, 3)}

      Select Case sender.name

          Case "Button1"

              ' Create On Screen Cells
              '
              Graphicgrid1.setCell(points, Color.Red)

              ' Off screen (Use Resize button to see them)
              '
              Graphicgrid1.setCell(New Point(9, 15), Color.White)
              Graphicgrid1.setCell(New Point(10, 15), Color.Green)
              Graphicgrid1.setCell(New Point(11, 15), Color.Yellow)
              Graphicgrid1.setCell(New Point(12, 15), Color.Gold)

          Case "Button2"

              ' Toggle the grid
              '
              Graphicgrid1.ShowGrid = Not Graphicgrid1.ShowGrid

          Case "Button3"

              ' Toggle Big or small size
              '
              If Graphicgrid1.Cells.Equals(New Size(20, 20)) Then
                  Graphicgrid1.Cells = New Size(10, 10)
              Else
                  Graphicgrid1.Cells = New Size(20, 20)
              End If

          Case "Button4"

              ' Remove a cell
              '
              Graphicgrid1.removeCell(New Point(2, 1))

          Case "Button5"

              ' Clear all the cells
              '
              Graphicgrid1.clearCells()

      End Select
  End Sub

That's it, nothing more to do. This is a simple, simple, simple application. I added another button "Button6" its in the code for GraphicGrid_Sample.zip. Its just a light show the cells turn on and off in random order and change color.

Sample Game

Sample game screenshot

"Wack a Mole" (click the bunny), but watch out he goes faster as time goes on! This is a really simple game I made just to demonstrate the control. The code is very simple.

VB.NET
' A few bars to keep track of things durring the game
'
Public Cell As Point       ' Last location where we put the cell
Public Gothim = False      ' Did we click of the bunny?


' This is the start and stop button on the form
'
Private Sub startStop_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles startStop.Click


    ' Start a nice slow game
    '
    Timer.Interval = 2000

    If Not Timer.Enabled Then


        ' Reset and start the game
        '
        BunniesMissed.Text = -1
        BunniesHit.Text = 0
        Timer.Start()


    Else


        ' Stop the game
        '
        Timer.Stop()
    End If
End Sub




' Find a new cell for the bunny and put him in it.
'
Private Sub redraw()
    Dim rnd As New Random
    Dim newPoint As Point


    ' Reset inGame vars
    '
    Gothim = False


    ' Generate a new point but make sure it's not the same as the old
    ' point
    '
    Do
        newPoint = New Point(rnd.Next(0, 4), rnd.Next(0, 2))
    Loop While newPoint.Equals(Cell)


    ' Draw the cell
    '
    Cell = newPoint
    Graphicgrid1.clearCells()
    Graphicgrid1.setCell(New Point(Cell.X, Cell.Y), Images.Images(0))
End Sub




' The timer went off, did we get the bunny?
'
Private Sub Timer_Tick(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles Timer.Tick



    If Not Gothim Then BunniesMissed.Text += 1


    If BunniesMissed.Text = 10 Then
        Timer.Stop()
        MsgBox("You stink!", MsgBoxStyle.Exclamation, "Bunnie Hunter")
    Else
        redraw()
    End If
End Sub

' This is where the magic happens.
' They clicked the grid and fired off this event.
'
Private Sub Graphicgrid1_gridClick( _
    ByVal sender As Object, ByVal GridPoint As _
    System.Drawing.Point) Handles Graphicgrid1.gridClick

    ' Where they clicked, is this where the bunny is?
    '
    If GridPoint.Equals(Cell) Then    ' Yup

        ' Change the bitmap to splater bunny
        '
        Graphicgrid1.removeCell(Cell)
        Graphicgrid1.setCell(New Point(Cell.X, Cell.Y), _
                Images.Images(1))

        ' Game stuff
        '
        Gothim = True
        BunniesHit.Text += 1
        Timer.Interval -= 25

    Else       ' Humm, the clicked but no bunny here

        ' Clicked on the wrong cell
        '
        BunniesMissed.Text += 1

    End If
End Sub

Todo

  1. Add an edit feature to the GraphicGrid (when you mouseover the grid it hilights the cell).
  2. Enhance the Drag and Drop features

Summery

I hope you enjoy using this control. If you have any ideas or anything you would do to make it better please let me know. Also if you have created anything using this control I would appreciate a mention in the credits :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I started programming for fun when I was about 10 on an Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)

The way I learned was by example, now its time to give back to the next generation of coders.



Comments and Discussions

 
Questionexcellent Pin
Southmountain2-Jul-22 10:16
Southmountain2-Jul-22 10:16 
QuestionMove selection Pin
Member 1164419610-May-15 21:19
Member 1164419610-May-15 21:19 
QuestionDirectory structure in grid? Pin
tauferre25-Aug-11 2:43
tauferre25-Aug-11 2:43 
GeneralGraphicGrid C# migration Pin
UFAnders13-Jul-08 18:32
UFAnders13-Jul-08 18:32 
GeneralA Very good Graphic Grid Pin
lukebrincat27-May-08 5:55
lukebrincat27-May-08 5:55 
GeneralVS2005 No add/rem item @ toolbox but Pin
ideve2-May-07 9:14
ideve2-May-07 9:14 
GeneralCreating components Pin
NUMBER1PEEP10-Feb-07 5:29
NUMBER1PEEP10-Feb-07 5:29 
GeneralImage or Bitmap movement Pin
Member 368875521-Jan-07 21:59
Member 368875521-Jan-07 21:59 
GeneralHighlight Copy Past Pin
lysis_lysis31-Oct-06 19:23
lysis_lysis31-Oct-06 19:23 
Yep this is possible but you must use mouse move, when you do every pixel in a cell is an event. I found a way around this, the code is long in the dll and my runtime. Just make sure your code only picks one pixel and no more in a cell, and you need code for the four quadrants - top left - top right - bottom right - bottom left if selecting more than one cell, I wrote this code and put it into the graphicgrid class. Here's somthing to get you wondering and thinking: Lysis. Blush | :O

'Step One:
'---------
'Create a form,
'Add a grid or two,
'Open up the code editor
'Add - Dim CellBorder As Boolean = True
'
'Step Two:
'---------
'Open up the graphicgrid.VB* control and replace everthing with the following, don't
'change the cCell.VB code:
'
'You now have HIGHLIGHT and CROSS HAIRS on the grids - Enjoy - LYSIS 20006
'==============================================================================
'
Public Class graphicgrid
Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl1 overrides dispose to clean up the component list.
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

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents grid As System.Windows.Forms.PictureBox
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
Me.grid = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'grid
'
Me.grid.Dock = System.Windows.Forms.DockStyle.Fill
Me.grid.Location = New System.Drawing.Point(0, 0)
Me.grid.Name = "grid"
Me.grid.Size = New System.Drawing.Size(128, 88)
Me.grid.TabIndex = 0
Me.grid.TabStop = False
'
'graphicgrid
'
Me.Controls.Add(Me.grid)
Me.Name = "graphicgrid"
Me.Size = New System.Drawing.Size(128, 88)
Me.ResumeLayout(False)

End Sub

#End Region

' Define Events, for descriptions see the event subs
'
Public Event gridClick(ByVal sender As Object, ByVal GridPoint As Point)
Public Event gridDoubleClick(ByVal sender As Object, ByVal GridPoint As Point)
Public Event gridMouseMove(ByVal sender As Object, ByVal GridPoint As Point)
Public Event gridMouseUp(ByVal sender As Object, ByVal e As Windows.Forms.MouseEventArgs, ByVal GridPoint As Point)
Public Event gridMouseDown(ByVal sender As Object, ByVal e As Windows.Forms.MouseEventArgs, ByVal GridPoint As Point)
' A few vars to hold custom Properties
'
Public propShowGrid As Boolean = True
Public propGridColor As Color = Color.White
Public propCells As New Size(10, 10) 'Number Cells X Y - Default is (10, 10)
Public CellSize As Size 'Size = Grid Size X and Y
Public CellContains As New cCell
'Use for Highlight 1st grid clicked
Public propCellBorder As Boolean = False 'Highlight - Boolean Switch
Public propCrossHair As Boolean = False 'Cross Hairs - Boolean Switch
''Private Sub Graphicgrid2_MouseMove(ByVal sender As System.Object, ByVal GridPoint As System.Drawing.Point) Handles Graphicgrid2.gridMouseMove
'
' Events, make sure to update the grid when these happen
'
Private Sub graphicgrid_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.BackColorChanged
setGrid()
End Sub
Private Sub graphicgrid_ForeColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ForeColorChanged
setGrid()
End Sub
Private Sub graphicgrid_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
setGrid()
End Sub
Private Sub graphicgrid_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
setGrid()
End Sub
Private Sub graphicgrid_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Move
setGrid()
End Sub
Private Sub graphicgrid_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
setGrid()
End Sub
' Properties
'
Public Property CellBorder() As Boolean 'Toggle - Call From Code - Show highlight if true...
Get
Return propCellBorder
End Get
Set(ByVal Value As Boolean)
propCellBorder = Value
setGrid()
End Set
End Property
Public Property CrossHair() As Boolean 'Toggle - Call From Code - Draw Cross Hairs if true...
Get
Return propCrossHair
End Get
Set(ByVal Value As Boolean)
propCrossHair = Value
setGrid()
End Set
End Property
Public Property ShowGrid() As Boolean
Get
Return propShowGrid
End Get
Set(ByVal Value As Boolean)
propShowGrid = Value
setGrid()
End Set
End Property

Public Property GridColor() As Color
Get
Return propGridColor
End Get
Set(ByVal Value As Color)
propGridColor = Value
setGrid()
End Set
End Property

Public Property Cells() As Size 'Cells = Number of cells for X and Y, Name = graphicGrid
Get
Return propCells
End Get
Set(ByVal Value As Size) 'PropCells = Me.Grid.Size
propCells = Value
setGrid()
End Set
End Property

' Sets the background image of the grid
'
Private Sub graphicgrid_BackgroundImageChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.BackgroundImageChanged
grid.BackgroundImage = Me.BackgroundImage
End Sub

' Resets or Applies new grid settings
'
Private Sub setGrid()
grid.BackColor = Me.BackColor
grid.ForeColor = Me.ForeColor

CellSize.Width = Me.Width / propCells.Width 'CellSize.Width = Grid.Width / Number of cells wide
CellSize.Height = Me.Height / propCells.Height

grid.Invalidate()
End Sub

' Sub: setCell
' Diz: Sets the contents of the cell by adding the attributes
' to the collection.
' Use: object.setCell(new point(1, 1), color.red)
'
Public Overloads Sub setCell(ByVal Cell As Point, ByVal newColor As Color)
CellContains.add(Cell, newColor)
grid.Invalidate()

End Sub

' Sub: setCell
' Diz: Sets the contents of a range of cells by adding the attributes
' to the collection.
' Use: object.setCell({new point(1, 1), new point(1, 2)}, color.red)
'
Public Overloads Sub setCell(ByVal Cells As Point(), ByVal newColor As Color)
Dim tmpPoint As Point
For Each tmpPoint In Cells
CellContains.add(tmpPoint, newColor)
Next
grid.Invalidate()
End Sub

' Sub: setCell
' Diz: Sets the contents of the cell by adding the attributes
' to the collection.
' Use: object.setCell(new point(1, 1), new bitmap("cell.bmp"))
'
Public Overloads Sub setCell(ByVal Cell As Point, ByVal newBitmap As Bitmap)
CellContains.add(Cell, newBitmap)
grid.Invalidate()

End Sub

' Sub: setCell
' Diz: Sets the contents of a range of cells by adding the attributes
' to the collection.
' Use: object.setCell({new point(1, 1), new point(1, 2)}, new bitmap("cell.bmp"))
'
Public Overloads Sub setCell(ByVal Cells As Point(), ByVal newBitmap As Bitmap)
Dim tmpPoint As Point
For Each tmpPoint In Cells
CellContains.add(tmpPoint, newBitmap)
Next
grid.Invalidate()
End Sub

' Sub: removeCell
' Diz: Removes a cell by deleting the attributes from the collection
' Use: object.removeCell(new point(1, 1))
'
Public Overloads Sub removeCell(ByVal Cell As Point)
CellContains.remove(Cell)
grid.Invalidate()
End Sub

' Sub: removeCell
' Diz: Removes a range of cells by deleting the attributes from the collection
' Use: object.removeCell({new point(1, 1), new point(1, 2)})
'
Public Overloads Sub removeCell(ByVal Cells As Point())
Dim tmpPoint As Point
For Each tmpPoint In Cells
CellContains.remove(tmpPoint)
Next
grid.Invalidate()
End Sub

' Sub: clearCell
' Diz: Removes all the cells by deleting the attributes from the collection
' Use: object.clearCells
'
Public Sub clearCells()
CellContains.Clear()
grid.Invalidate()
End Sub

Dim MUPoint As Point 'New Point(0, 0)
Dim MMPoint As Point 'New Point(0, 0)
Dim MDPoint As Point 'New Point(0, 0)

' Event: grid_MouseDown
' Diz: When the user moves the mouse inside the grid
' Return: The mouse and A point structure indicating the grid's cell
'
Private Sub grid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grid.MouseDown
Dim clientPoint As System.Drawing.Point = Me.PointToClient(Me.MousePosition)
RaiseEvent gridMouseDown(Me, e, New Point(Int(clientPoint.X / CellSize.Width), Int(clientPoint.Y / CellSize.Height)))
'
'The following MDPoint returns top left X and Y for clicked cell
' WORKING....
If CellBorder = True Then
Dim myPointX As Integer
Dim myPointY As Integer
myPointX = clientPoint.X - (CellSize.Width / 2)
myPointY = clientPoint.Y - (CellSize.Height / 2)
myPointX = myPointX / CellSize.Width
myPointY = myPointY / CellSize.Height
MDPoint = New Point(Int(myPointX * CellSize.Width), Int(myPointY * CellSize.Height))
End If
End Sub
'Dim MDPoint2 As Point 'Negative of mouse down
' Event: grid_MouseMove
' Diz: When the user moves the mouse inside the grid
' Return: A point structure indicating the grid's cell
'
Private Sub grid_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grid.MouseMove
Dim clientPoint As System.Drawing.Point = Me.PointToClient(Me.MousePosition)
RaiseEvent gridMouseMove(Me, New Point(Int(clientPoint.X / CellSize.Width), Int(clientPoint.Y / CellSize.Height)))
'
'The following MMPoint returns lower right X and Y for clicked cell
' WORKING....
If CellBorder = True Then
Dim myPointX As Integer
Dim myPointY As Integer
myPointX = clientPoint.X - (CellSize.Width / 2)
myPointY = clientPoint.Y - (CellSize.Height / 2)
myPointX = myPointX / CellSize.Width
myPointY = myPointY / CellSize.Height
MMPoint = New Point(Int(myPointX * CellSize.Width + CellSize.Width), Int(myPointY * CellSize.Height + CellSize.Height))
End If
End Sub

' Event: grid_MouseUp
' Diz: When the user moves the mouse inside the grid
' Return: The mouse and A point structure indicating the grid's cell
'
Private Sub grid_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grid.MouseUp
Dim clientPoint As System.Drawing.Point = Me.PointToClient(Me.MousePosition)
RaiseEvent gridMouseUp(Me, e, New Point(Int(clientPoint.X / CellSize.Width), Int(clientPoint.Y / CellSize.Height)))
If CellBorder = True Then
MUPoint = New Point(e.X, e.Y)
End If
End Sub
'Point = New Point(0, 0)
' Event: grid_Paint
' Diz: Paints the grid
'
Private Sub grid_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles grid.Paint
Dim contents As cCell
Dim x As Integer
Dim y As Integer

' If there are any custom attributes draw them out
'
For Each contents In CellContains
If contents.cellbitmap Is Nothing Then
e.Graphics.FillRectangle(New SolidBrush(contents.cellColor), contents.cellPoint.X * CellSize.Width, _
contents.cellPoint.Y * CellSize.Height, CellSize.Width, CellSize.Height)
Else
e.Graphics.DrawImage(contents.cellbitmap, contents.cellPoint.X * CellSize.Width, _
contents.cellPoint.Y * CellSize.Height, CellSize.Width, CellSize.Height)
End If
Next

' If they want the grid draw that as well
'
If ShowGrid = True Then
For x = 0 To Cells.Width
For y = 0 To Cells.Height
e.Graphics.DrawRectangle(New Pen(GridColor), x * CellSize.Width, y * CellSize.Height, CellSize.Width, CellSize.Height)
Next
Next
End If
'
'
'XXXXXXXXXXXX Grid HIGHLIGHT and BORDER COLOR XXXXXXXXXXXX By Eric Blake 2006 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'WORKING - Pens - Style - Color - Size:
Dim gridUpperXY As Point = New Point(0, 0)
Dim gridLowerXY As Point = New Point(CellSize.Width * propCells.Width, CellSize.Height * propCells.Height)
Dim myColorF As Color = Color.FromArgb(100, 192, 156, 214) 'Change the FILL color here
Dim myColor1 As Color = Color.FromArgb(150, 255, 255, 255) 'Change 1st cell color here
Dim myPen As New Pen(Color.FromArgb(255, 40, 20, 194), 2) 'Top Border Color - pen attributes
Dim myPenL As New Pen(Color.Cornsilk, 2) 'Lower Border Color - pen attributes
Dim myPenB As New Pen(Color.FromArgb(255, 70, 130, 180), 2) '(255, 40, 20, 194), 2) 'LightSteelGrey - Grid Border - pen attributes
Dim myPenCrossHair As New Pen(Color.Red, 0.25) 'FromArgb(255, 189, 24, 240), 1)
myPen.DashStyle = Drawing2D.DashStyle.Dot
myPenL.DashStyle = Drawing2D.DashStyle.Solid
myPenB.DashStyle = Drawing2D.DashStyle.Solid
'
'Grid Border Color
e.Graphics.DrawRectangle(myPenB, gridUpperXY.X, gridUpperXY.Y, gridLowerXY.X, gridLowerXY.Y)
'
'Highlight.......Start..........................................................................................................

'Highlighted Rectangle fill color
'First cell of selection is transparent
'Border = two colors, one on top of the other - Lower drawn first
'
'WORKING Draw rectangle if x and y are positive of 0 when mouse moves
If CellBorder = True Then
Dim pt1x As Integer = MDPoint.X 'Top Left
Dim pt1y As Integer = MDPoint.Y '
Dim pt2x As Integer = MMPoint.X 'Bottom Right
Dim pt2y As Integer = MMPoint.Y
If pt2x <> 0 Or pt2y <> 0 Then 'Make sure nothing happens till mouse move has a value.
If pt2x > pt1x And pt2y > pt1y Then 'From mouse down to positive xy numbers
e.Graphics.FillRectangle(New SolidBrush(myColorF), _
Math.Min(pt2x + 1, pt1x + 1), _
Math.Min(pt2y + 1, pt1y + 1), _
Math.Abs(pt2x - pt1x - 1), _
Math.Abs(pt2y - pt1y - 1))
e.Graphics.FillRectangle(New SolidBrush(myColor1), _
MDPoint.X + 1, _
MDPoint.Y + 1, _
CellSize.Width - 1, _
CellSize.Height - 1)
e.Graphics.DrawRectangle(myPenL, _
Math.Min(pt2x, pt1x), _
Math.Min(pt2y, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.DrawRectangle(myPen, _
Math.Min(pt2x, pt1x), _
Math.Min(pt2y, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
'If CrossHair = True Then 'If true draw both cross hairs
Dim halfX As Integer = pt2x - CellSize.Width / 2
e.Graphics.DrawLine(myPenCrossHair, halfX, 0, halfX, Size.Height)
Dim halfy As Integer = pt2y - CellSize.Height / 2
e.Graphics.DrawLine(myPenCrossHair, 0, halfy, Size.Width, halfy)
'End If
End If
'WORKING Draw rectangle if x and y are Negative of 0 when mouse moves
If pt2x < pt1x + CellSize.Width And pt2y < pt1y + CellSize.Height * 2 Then
pt2x = pt2x - CellSize.Width * 2
pt2y = pt2y - CellSize.Height * 2
e.Graphics.FillRectangle(New SolidBrush(myColorF), _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.FillRectangle(New SolidBrush(myColor1), _
MDPoint.X + 1, _
MDPoint.Y + 1, _
CellSize.Width - 1, _
CellSize.Height - 1)
e.Graphics.DrawRectangle(myPenL, _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.DrawRectangle(myPen, _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
Dim halfX As Integer = pt2x + CellSize.Width / 2 + CellSize.Width
e.Graphics.DrawLine(myPenCrossHair, halfX, 0, halfX, Size.Height)
Dim halfy As Integer = pt2y + CellSize.Height / 2 + CellSize.Height
e.Graphics.DrawLine(myPenCrossHair, 0, halfy, Size.Width, halfy)
End If
'WORKING Draw rectangle if X is Negative and Y is positive of 0 when mouse moves
If pt2x < pt1x + CellSize.Width And pt2y > pt1y Then
pt2x = pt2x - CellSize.Width * 2
e.Graphics.FillRectangle(New SolidBrush(myColorF), _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + 1, pt1y + 1), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.FillRectangle(New SolidBrush(myColor1), _
MDPoint.X + 1, _
MDPoint.Y + 1, _
CellSize.Width - 1, _
CellSize.Height - 1)
e.Graphics.DrawRectangle(myPenL, _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.DrawRectangle(myPen, _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
Dim halfX As Integer = pt2x + CellSize.Width / 2 + CellSize.Width
e.Graphics.DrawLine(myPenCrossHair, halfX, 0, halfX, Size.Height)
Dim halfy As Integer = pt2y - CellSize.Height / 2
e.Graphics.DrawLine(myPenCrossHair, 0, halfy, Size.Width, halfy)
End If
'WORKING Draw rectangle if Y is Negative and X is positive of 0 when mouse moves
If pt2x > pt1x And pt2y < pt1y + CellSize.Height Then
pt2y = pt2y - CellSize.Height * 2
e.Graphics.FillRectangle(New SolidBrush(myColorF), _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.FillRectangle(New SolidBrush(myColor1), _
MDPoint.X + 1, _
MDPoint.Y + 1, _
CellSize.Width - 1, _
CellSize.Height - 1)
e.Graphics.DrawRectangle(myPenL, _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
e.Graphics.DrawRectangle(myPen, _
Math.Min(pt2x + CellSize.Width, pt1x), _
Math.Min(pt2y + CellSize.Height, pt1y), _
Math.Abs(pt2x - pt1x), _
Math.Abs(pt2y - pt1y))
Dim halfX As Integer = pt2x - CellSize.Width / 2
e.Graphics.DrawLine(myPenCrossHair, halfX, 0, halfX, Size.Height)
Dim halfy As Integer = pt2y + CellSize.Height / 2 + CellSize.Height
e.Graphics.DrawLine(myPenCrossHair, 0, halfy, Size.Width, halfy)
End If
End If
Else
'Reset Mouse Position to zero
MDPoint = New Point(0, 0)
MMPoint = New Point(0, 0)
MUPoint = New Point(0, 0)
End If
'Dispose of pens
myPen.Dispose()
myPenL.Dispose()
myPenB.Dispose()
myPenCrossHair.Dispose()
End Sub
' Event: grid_Click
' Diz: When the user clicks inside the grid
' Return: A point structure indicating the grid's cell
'

Private Sub grid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grid.Click
Dim clientPoint As System.Drawing.Point = Me.PointToClient(Me.MousePosition)
RaiseEvent gridClick(Me, New Point(Int(clientPoint.X / CellSize.Width), Int(clientPoint.Y / CellSize.Height)))
End Sub
' Event: grid_DoubleClick
' Diz: When the user clicks inside the grid
' Return: A point structure indicating the grid's cell
'
Private Sub grid_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles grid.DoubleClick
Dim clientPoint As System.Drawing.Point = Me.PointToClient(Me.MousePosition)
RaiseEvent gridDoubleClick(Me, New Point(Int(clientPoint.X / CellSize.Width), Int(clientPoint.Y / CellSize.Height)))
End Sub

End Class
Generalget the image name from a cell Pin
ntib30-Jun-06 5:06
ntib30-Jun-06 5:06 
QuestionGet property from a single cell? Pin
Qvicksilver28-Jun-05 3:13
Qvicksilver28-Jun-05 3:13 
AnswerRe: Get property from a single cell? Pin
lysis_lysis8-Dec-05 18:12
lysis_lysis8-Dec-05 18:12 
AnswerRe: Get property from a single cell? Pin
TimCarr27-Mar-07 5:52
TimCarr27-Mar-07 5:52 
GeneralAnyone used it in asp.net yet. Pin
James Whit...15-Apr-05 8:47
James Whit...15-Apr-05 8:47 
GeneralScroll Pin
Member 93505225-Feb-05 4:55
Member 93505225-Feb-05 4:55 
GeneralRe: Scroll Pin
lysis_lysis29-Oct-05 18:00
lysis_lysis29-Oct-05 18:00 
GeneralEach individual cell Pin
etchan26-Dec-04 14:36
etchan26-Dec-04 14:36 
QuestionRe: Each individual cell Pin
DannSmith23-Oct-06 6:18
DannSmith23-Oct-06 6:18 
GeneralDrawing tools Pin
Himal Patel4-Oct-04 2:41
Himal Patel4-Oct-04 2:41 
GeneralDraw letters and graphs Pin
mauleTTT19-Sep-04 11:42
mauleTTT19-Sep-04 11:42 
GeneralCompitable version of .NET Pin
Swapnil Pithwa20-Aug-04 5:25
Swapnil Pithwa20-Aug-04 5:25 
GeneralRe: Compitable version of .NET Pin
Matthew Hazlett20-Aug-04 6:21
Matthew Hazlett20-Aug-04 6:21 
GeneralRe: Compitable version of .NET Pin
KevJB20-Oct-04 21:10
KevJB20-Oct-04 21:10 
GeneralAdding a button to the Grid Pin
opticnurve14-May-04 8:25
opticnurve14-May-04 8:25 
GeneralRe: Adding a button to the Grid Pin
Matthew Hazlett14-May-04 8:30
Matthew Hazlett14-May-04 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.