views:

109

answers:

3

I would like advice on whether or not I should program the following from scratch myself or use a 3rd party component. If there is a 3rd party component that fits the bill without too much baggage I'd appreciate recommendations.

I would like to have a component that is a matrix of cells, like a grid control. I need no editing, no selection, no dynamic resizing of columns, no default cell drawing behaviour,no effects when the mouse moves over things, no keyboard shortcuts.

All I need is to be able to do is to draw each cell myself and if the size of the grid exceeds the available space of the parent component then scroll bars should appear.

In other words I would like to be able to say: grid.Invalidate()

and then I get a callback for each visible cell something like this:

void DrawCell(int x, int y, Graphics g, Rectangle cellDrawingRect)

A: 

This seems pretty trivial if you just write your own control (derive from ScrollableControl). Handle Control.Paint to draw your cells and use the HorizontalScroll and VerticalScroll properties to adjust your scroll bars.

Ron Warholic
+2  A: 

My advice? Calculate the size of the grid, make an image that size, draw your grid, and put the image in a PictureBox, and put that PictureBox in a ScrollableControl.

You don't need to mess with Control.Paint as the picturebox handles that for you.

If you're so inclined, you could put all this in a UserControl. This makes your code a little more reusable and more portable.

Charlie Salts
using this method is it also possible to smooth update bits and pieces of the image the picturebox is showing or does it have to be completely replaced with a new image each time?
freddy smith
No, you can re-draw on the same image. Just make sure you call PictureBox.Invalidate() when you're done drawing.
Charlie Salts
A: 

Here is an earlier rant of mine on exactly this subject:

http://stackoverflow.com/questions/1342689/need-help-creating-control-to-display-data/1342891#1342891

I would definitely recommend doing this yourself, especially since it's essentially a read-only control. Most off-the-shelf grid components are built to do a lot more than what you need, and wrestling them into the shape you need could easily take more effort than writing your own from scratch.

As Charlie Salts mentioned, it might be easier to draw the entire thing on a big PictureBox and then move it around on a smaller Panel, but this would depend on how big the overall grid has to be. Bitmaps can take up a lot of memory.

MusiGenesis
Yeah I forgot about the bitmap size. That's one good reason why someone should handle Control.Paint instead.
Charlie Salts
not too large. perhaps maximum 2500x2500 pixels. The viewable subset of this would be anything less than or equal to a standard window size.
freddy smith
@freddy: that's 25 MB or so of Bitmap, which is probably fine for your purposes. I do a lot of work in .Net Compact Framework, where a Bitmap that big would be catastrophic.
MusiGenesis