Visual Basic >
Visual Basic Forums
>
Visual Basic Language
>
Custom grid control using GDI + for Windows Forms
Custom grid control using GDI + for Windows Forms
- I have to design a custom "log grid" control to use in another of my projects.
Up front, I am new to vb.net 2008. I have used VB6 for years and I am trying to learn how to resove the differences as I go. But I need help.
This log grid contains 96 columns and 4 rows.
I tried using the TableLayoutPanel and a Panel per cell but found the control to slow to paint. That's 384 discreet controls.
I am now trying a usercontrol with a panel and GDI+ to create the grid. I've created a collection for the columns and a collection for the rows. So far so good. My mouse clicks raise the correct events per cell.
I now need to indicate which column the mouse is over. This is the problem. The column must be highlighted by a vertical bar from the top of the column to the bottom. It must also be transparent. I need to see the gridlines under the highlight.
I've tried using region using the following code. (myRegion is declared Private MyRegion as Region at module level for persistance)
The idea behind this code is if the region exists delete it, then create the new region and fill it. The results are quite different thought. The region is not transparent until I move over another control on my control. (I've placed a button on my control to test certain code results). Once I do this, the region becomes transparent. The delete portion of my code does not work either. Everytime I move over another column a new region is painted (this is correct behavior) but the last column region is not deleted and I end up with the control covered with these solid regions.Dim H As Integer = Me.Height
Dim W As Integer = Me.Width
Static L As Integer = 0
Static R As Integer = 0
Dim myPen As New System.Drawing.Pen(Color.Black) 'GridColor)
Dim formGraphics As System.Drawing.Graphics
Dim solidBrush As New SolidBrush(Color.Black)
Dim mySolidBrush As New SolidBrush(Color.LightGreen)
Dim i As Integer = 1
Dim c As clsCol
formGraphics = pnlCtlLog.CreateGraphics()
If e.X >= L And e.X < R Then Exit Sub Else Try myRegion.Dispose() Catch ex As Exception End Try End If For i = 1 To Cols.Count c = Cols.Item(i) If e.X >= c.Left And e.X < c.Right Then myRegion = New [Region](New Rectangle(c.Left, 0, c.Right - c.Left, H)) L = c.Left R = c.Right solidBrush.Color = Color.LightGreen formGraphics.FillRegion(solidBrush, myRegion) pnlCtlLog.Focus() Exit For End If Next<br/>
When I use Me.refrsh is causes the control to flicker and the region is still opaque.
BTW, this code is executed from the Mouse_Move event.
I hope I have been clear about what I am trying to accomplish.
Thanks
Answers
- Out of curiosity why not use a DataGridView for this?
VS2008 comes with a full set of usefull controls that you can use out of the box.
Please remember to mark the replies as answers if they help you.- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 2:36 AM
- Ok. Also consider taking a look at Windows Presentation Foundation. When it comes to object design and transformation you would be amazed on what you can do. You can host a WPF control on a Windows form.
Another alternative is to consider investing in a third party component. There are some powerfull components out that that can give you some ideas on doing something similar or better.
Personally I've used Infragistics and DevExpress. Its an investment but well worth it.The time I saved using theior components is ridiculous if we consider the time I would need to spend to get 10% of the level of functionalities their components manage to give you out of the box. There are also the Telerik ones that seems quite nice.
http://www.infragistics.com
http://www.devexpress.com
http://www.telerik.com/
For a few hundred bucks you get a whole set of components. Get the trials and see the demos.
Cheers
Please remember to mark the replies as answers if they help you.- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 2:36 AM
- Thanks guys.
I've decided to use a panel for the column highlight and paint the covered grid lines on this panel.
This is a very simple user interface. I figure if I need to draw gridlines anyways I might as well design the control.
I'm also going to look at sublcassing with GDI+ Rectangles, but this could be a future improvement on this control. However, if anyone has any ideas on subclassing graphics, I am open to here about it now. Otherwise this problem is solved.
Thanks Don- Marked As Answer bydrh6558 Monday, November 09, 2009 11:57 PM
All Replies
- Out of curiosity why not use a DataGridView for this?
VS2008 comes with a full set of usefull controls that you can use out of the box.
Please remember to mark the replies as answers if they help you.- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 2:36 AM
- The borders of the cells are not standard grid borders. They are different lengths depending on the col and cell location. Each row of the grid ends up looking like the graduation ticks on a ruler.
I origanaly looked at the DataGridView and could not get it to do what I wanted. Having said that, I had not yet considered using GDI+ to paint the grid lines. I will revisit DataGridView and see if I can get it to work and let you know.
Thanks - Ok. Also consider taking a look at Windows Presentation Foundation. When it comes to object design and transformation you would be amazed on what you can do. You can host a WPF control on a Windows form.
Another alternative is to consider investing in a third party component. There are some powerfull components out that that can give you some ideas on doing something similar or better.
Personally I've used Infragistics and DevExpress. Its an investment but well worth it.The time I saved using theior components is ridiculous if we consider the time I would need to spend to get 10% of the level of functionalities their components manage to give you out of the box. There are also the Telerik ones that seems quite nice.
http://www.infragistics.com
http://www.devexpress.com
http://www.telerik.com/
For a few hundred bucks you get a whole set of components. Get the trials and see the demos.
Cheers
Please remember to mark the replies as answers if they help you.- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 2:36 AM
- Thanks guys.
I've decided to use a panel for the column highlight and paint the covered grid lines on this panel.
This is a very simple user interface. I figure if I need to draw gridlines anyways I might as well design the control.
I'm also going to look at sublcassing with GDI+ Rectangles, but this could be a future improvement on this control. However, if anyone has any ideas on subclassing graphics, I am open to here about it now. Otherwise this problem is solved.
Thanks Don- Marked As Answer bydrh6558 Monday, November 09, 2009 11:57 PM


