I have drawn a grid in vb2008 so that each rectangle represents a 15 min period of a day. Time slots are shown by filling in the grid in different colours using the draw rectangle method. No problem so far. Now I want to discover the colour of any given part of the grid to prevent an overdraw. That is the problem. I do not know how to see if say pixel(x,y) is green or blue of anything. Can someone please give me the coding?
Thanks

Hi Brissac

Based on my understanding from your Q ...

You might have a list of object holding the coordinates of rectangle with color .. Go thro the following

Imports System.Drawing

Public Class Form1
    Dim ColouredRect As List(Of GridRect)

    Private Class GridRect
        Dim _RectLocation As System.Drawing.Point
        Dim _RectColour As System.Drawing.Pen

        Public ReadOnly Property RectLocation() As Point
            Get
                Return _RectLocation
            End Get
        End Property

        Public ReadOnly Property RectColour() As Pen
            Get
                Return _RectColour
            End Get
        End Property

        Public Sub New(ByVal A As Pen, ByVal B As Point)
            _RectColour = A
            _RectLocation = B
            '---- ur code to initialize for a list of object
        End Sub
    End Class

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        For Each itm In ColouredRect
            If New Rectangle(itm.RectLocation, New Size(20, 20)).Contains(e.Location) = True Then
                ToolTip1.ForeColor = itm.RectColour.Color
                'ToolTip1.SetToolTip(Me, itm.RectColour.ToString)
                ToolTip1.Show("You have to Link Your Procedure for what to be displayed in the tool tip", Panel1)
                Exit Sub
            Else
                ToolTip1.SetToolTip(Panel1, e.Location.ToString)
                ToolTip1.Show(e.Location.ToString, Panel1)
            End If
        Next
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        For Each itm In ColouredRect
            e.Graphics.DrawRectangle(itm.RectColour, New Rectangle(itm.RectLocation, New Size(20, 20)))
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ColouredRect = New List(Of GridRect)()
        '--- initialize the object with your code

        ColouredRect.Add(New GridRect(Pens.Blue, New Point(20, 20)))
        ColouredRect.Add(New GridRect(Pens.Brown, New Point(20, 50)))
        ColouredRect.Add(New GridRect(Pens.DarkCyan, New Point(20, 80)))
        ColouredRect.Add(New GridRect(Pens.Red, New Point(20, 110)))

        Panel1.Focus()
    End Sub
End Class

This is my code based on my understanding..... I think it might help u
I dont understand exactly what u r planning to do


Rgrds

Sam

Member Avatar for Unhnd_Exception

I would do my drawing on a bitmap.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Draw your grid on a picture box's image
    'Make the picture box the size of the grid and then create an image that size
    PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
End Sub

Private Sub DrawGrid()
    Dim Graphics As Graphics = Graphics.FromImage(PictureBox1.Image)
    'Draw your grid on the image
    Graphics.Dispose()
End Sub

Private Function GetColor(ByVal x As Integer, ByVal y As Integer) As Color
   'Validate the image isnot nothing and that the x and y fall within 
   'the image bounds
   Return CType(PictureBox1.Image, Bitmap).GetPixel(x, y)
End Function

Thanks a lot Unhnd_Exception. This seems like an excellent idea. Will try it.
Brissac

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.