msdn.vb.lang forum grid

G

Guest

Hey all,

You notice how when you mouse over each thread in this forum how the
highlights, well how can I do this same thing in my win form with my datagrid?

thanks,
rodchar
 
K

Ken Tucker [MVP]

Hi,

You need to make your own columnstyle for that. Here is a column i
am working on that does that.

Code

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms

Imports System.Reflection

Imports System.ComponentModel

Public Class HotTrackTextBoxColumn

Inherits DataGridTextBoxColumn

Dim c As Integer

Dim rectPaint As New RectangleF

Dim fnt As New Font(MyBase.TextBox.Font.Name, MyBase.TextBox.Font.Size,
FontStyle.Underline)

Dim WithEvents dg As DataGrid

Dim oldCell As New Point(-1, -1)

Dim isInCell As Boolean = False

Public Sub HandleMouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles dg.MouseMove

Dim g As Graphics = dg.CreateGraphics

Dim bounds As Rectangle

Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))

isInCell = (hti.Row > -1 And hti.Column = c)

Static bRedraw As Boolean = False

If isInCell Then

Dim pt As Point

pt = New Point(hti.Row, hti.Column)

If Not pt.Equals(oldCell) Then

'

' Create a region where we want the datagrid to redraw

' So the datagrid doesn't flash.

'

Dim pthToRedraw As New Drawing2D.GraphicsPath

Dim rgnToRedraw As Region

Dim rCell As Rectangle = dg.GetCellBounds(pt.X, pt.Y)

pthToRedraw.AddRectangle(rCell)

If oldCell.X > -1 Then

'

' Have to redraw last cell

'

pthToRedraw.AddRectangle(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

rgnToRedraw = New Region(pthToRedraw)

dg.Invalidate(rgnToRedraw)

End If

'

' Flag datagrid for redraw

'

bRedraw = True

oldCell = pt

Else

'

' Only redraw when needed

'

If bRedraw Then

dg.Invalidate(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

bRedraw = False

oldCell = New Point(-1, -1)

End If

End Sub



Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

'

' First time we paint get a reference to datagrid

' So we can consume its events

'

If Not bPainted Then

dg = Me.DataGridTableStyle.DataGrid

c = -1

For Each grdCol As DataGridColumnStyle In
Me.DataGridTableStyle.GridColumnStyles

c += 1

If grdCol.MappingName = Me.MappingName Then Exit For

Next

End If

bPainted = True

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

Dim pnHot As New Pen(SystemColors.HotTrack)

Dim brHot As New SolidBrush(Color.FromArgb(128, SystemColors.HotTrack))

' see through brush

If MouseOverCell(rowNum) Then

bounds.Inflate(-1, -1)

g.FillRectangle(brHot, bounds)

g.DrawRectangle(pnHot, bounds)

End If



Dim r As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)

rectPaint = r

End Sub

Public ReadOnly Property MouseOverCell(ByVal rownum As Integer) As Boolean

Get

Dim pt As New Point(rownum, c)

Return pt.Equals(oldCell)

End Get

End Property

End Class


Link to example
http://www.onteorasoftware.com/Downloads/datagridcolumnstyles.zip

Ken
-----------------------
Hey all,

You notice how when you mouse over each thread in this forum how the
highlights, well how can I do this same thing in my win form with my
datagrid?

thanks,
rodchar
 
G

Guest

I appreciate it.

Ken Tucker said:
Hi,

You need to make your own columnstyle for that. Here is a column i
am working on that does that.

Code

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms

Imports System.Reflection

Imports System.ComponentModel

Public Class HotTrackTextBoxColumn

Inherits DataGridTextBoxColumn

Dim c As Integer

Dim rectPaint As New RectangleF

Dim fnt As New Font(MyBase.TextBox.Font.Name, MyBase.TextBox.Font.Size,
FontStyle.Underline)

Dim WithEvents dg As DataGrid

Dim oldCell As New Point(-1, -1)

Dim isInCell As Boolean = False

Public Sub HandleMouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles dg.MouseMove

Dim g As Graphics = dg.CreateGraphics

Dim bounds As Rectangle

Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))

isInCell = (hti.Row > -1 And hti.Column = c)

Static bRedraw As Boolean = False

If isInCell Then

Dim pt As Point

pt = New Point(hti.Row, hti.Column)

If Not pt.Equals(oldCell) Then

'

' Create a region where we want the datagrid to redraw

' So the datagrid doesn't flash.

'

Dim pthToRedraw As New Drawing2D.GraphicsPath

Dim rgnToRedraw As Region

Dim rCell As Rectangle = dg.GetCellBounds(pt.X, pt.Y)

pthToRedraw.AddRectangle(rCell)

If oldCell.X > -1 Then

'

' Have to redraw last cell

'

pthToRedraw.AddRectangle(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

rgnToRedraw = New Region(pthToRedraw)

dg.Invalidate(rgnToRedraw)

End If

'

' Flag datagrid for redraw

'

bRedraw = True

oldCell = pt

Else

'

' Only redraw when needed

'

If bRedraw Then

dg.Invalidate(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

bRedraw = False

oldCell = New Point(-1, -1)

End If

End Sub



Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

'

' First time we paint get a reference to datagrid

' So we can consume its events

'

If Not bPainted Then

dg = Me.DataGridTableStyle.DataGrid

c = -1

For Each grdCol As DataGridColumnStyle In
Me.DataGridTableStyle.GridColumnStyles

c += 1

If grdCol.MappingName = Me.MappingName Then Exit For

Next

End If

bPainted = True

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

Dim pnHot As New Pen(SystemColors.HotTrack)

Dim brHot As New SolidBrush(Color.FromArgb(128, SystemColors.HotTrack))

' see through brush

If MouseOverCell(rowNum) Then

bounds.Inflate(-1, -1)

g.FillRectangle(brHot, bounds)

g.DrawRectangle(pnHot, bounds)

End If



Dim r As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)

rectPaint = r

End Sub

Public ReadOnly Property MouseOverCell(ByVal rownum As Integer) As Boolean

Get

Dim pt As New Point(rownum, c)

Return pt.Equals(oldCell)

End Get

End Property

End Class


Link to example
http://www.onteorasoftware.com/Downloads/datagridcolumnstyles.zip

Ken
-----------------------
Hey all,

You notice how when you mouse over each thread in this forum how the
highlights, well how can I do this same thing in my win form with my
datagrid?

thanks,
rodchar
 
G

Guest

all the code goes behind my Form1 code behind?

Ken Tucker said:
Hi,

You need to make your own columnstyle for that. Here is a column i
am working on that does that.

Code

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms

Imports System.Reflection

Imports System.ComponentModel

Public Class HotTrackTextBoxColumn

Inherits DataGridTextBoxColumn

Dim c As Integer

Dim rectPaint As New RectangleF

Dim fnt As New Font(MyBase.TextBox.Font.Name, MyBase.TextBox.Font.Size,
FontStyle.Underline)

Dim WithEvents dg As DataGrid

Dim oldCell As New Point(-1, -1)

Dim isInCell As Boolean = False

Public Sub HandleMouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles dg.MouseMove

Dim g As Graphics = dg.CreateGraphics

Dim bounds As Rectangle

Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))

isInCell = (hti.Row > -1 And hti.Column = c)

Static bRedraw As Boolean = False

If isInCell Then

Dim pt As Point

pt = New Point(hti.Row, hti.Column)

If Not pt.Equals(oldCell) Then

'

' Create a region where we want the datagrid to redraw

' So the datagrid doesn't flash.

'

Dim pthToRedraw As New Drawing2D.GraphicsPath

Dim rgnToRedraw As Region

Dim rCell As Rectangle = dg.GetCellBounds(pt.X, pt.Y)

pthToRedraw.AddRectangle(rCell)

If oldCell.X > -1 Then

'

' Have to redraw last cell

'

pthToRedraw.AddRectangle(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

rgnToRedraw = New Region(pthToRedraw)

dg.Invalidate(rgnToRedraw)

End If

'

' Flag datagrid for redraw

'

bRedraw = True

oldCell = pt

Else

'

' Only redraw when needed

'

If bRedraw Then

dg.Invalidate(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

bRedraw = False

oldCell = New Point(-1, -1)

End If

End Sub



Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

'

' First time we paint get a reference to datagrid

' So we can consume its events

'

If Not bPainted Then

dg = Me.DataGridTableStyle.DataGrid

c = -1

For Each grdCol As DataGridColumnStyle In
Me.DataGridTableStyle.GridColumnStyles

c += 1

If grdCol.MappingName = Me.MappingName Then Exit For

Next

End If

bPainted = True

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

Dim pnHot As New Pen(SystemColors.HotTrack)

Dim brHot As New SolidBrush(Color.FromArgb(128, SystemColors.HotTrack))

' see through brush

If MouseOverCell(rowNum) Then

bounds.Inflate(-1, -1)

g.FillRectangle(brHot, bounds)

g.DrawRectangle(pnHot, bounds)

End If



Dim r As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)

rectPaint = r

End Sub

Public ReadOnly Property MouseOverCell(ByVal rownum As Integer) As Boolean

Get

Dim pt As New Point(rownum, c)

Return pt.Equals(oldCell)

End Get

End Property

End Class


Link to example
http://www.onteorasoftware.com/Downloads/datagridcolumnstyles.zip

Ken
-----------------------
Hey all,

You notice how when you mouse over each thread in this forum how the
highlights, well how can I do this same thing in my win form with my
datagrid?

thanks,
rodchar
 
C

Cor Ligthert

Rodchar,

I am still not sure if you are making programs for the web of for windows
forms, what is it, and when it is webform, I advice you to tell that in your
questions, I think I remember it when you answer it now. However in this
newsgroup is the winform default when not told.

Cor
 
C

Cor Ligthert

Rodchar,

Than you would not talk about:
ispostback
codebehind
That is typical webform terminology

In a windowform you have a design form and normalcode.

Ispostback does not exist.

You can real good confuse us.

:)))

Cor
 
G

Guest

You're right it is a win form project. So am i correct to say put it in the
code behind of the form or in a class by itself?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top