0

An example:

Using VS I can create MyControl which derives from UserControl, to this can be added a set of data as a member or property. Using the visual designer I can add a two panels to MyControl - each intended to render the same data but in a different form (FTSOA say a pie chart and a bar graph) using methods I intend to supply.

I think there are a number of approaches that will allow MyControl to use those methods to redraw the panels.

Firstly use VS to add a paint event for each panel. This is quick to implement and allows the methods access to MyControl data as they are created within the MyControl class.

Secondly to override the OnPaint method for each panel. AFAIK to do this requires creating a UserControl for each panel with the associated class. Then the OnPaint method of each class can be overridden. A downside is that each class has to be given access to MyControl data.

Thirdly it is also likely (I've not done this one) that overriding MyControl OnPaint and manually re-drawing each panel is also possible - but getting hold of the Graphics etc is an issue.

Question: I can get (1 & 2) to work, but I'd like to know what other people think.

Gray-M
  • 302
  • 2
  • 12

1 Answers1

1

Well, all of the above. But a Panel control certainly wasn't optimized to be a very good control for painting. It derives from ScrollableControl and that's what it is really good at, a container that can scroll its content. Also good as quick way to move a group of controls. Or hide them. Or disable them.

It is not exactly ideal for painting. It doesn't double-buffer, turning that on requires deriving your own control from it. And it also optimizes for container behavior, you'll need to turn on the ControlStyles.ResizeRedraw style to get the Paint event to fire when it resizes. If you actually need custom painting that also needs to be scrollable then Panel is a good base class to derive from.

Sounds to me you should really override the UserControl's OnPaint() method. Minimizing the number of controls is a very strong optimization goal, they are really expensive.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Ah. Well for drawing graphs and stuff maybe I should have chosen a picture box. I take it you prefer the former as it uses less class objects derived from UserForm. – Gray-M Aug 16 '12 at 17:43
  • After some more investigation I've decided to stick with a Panel control. I still feel this is not perfect but I want to draw graph type lines and have the data supplied by the owning control and to respond to mouse events including the scroll wheel (pan/zoom/...) so it needs to have focus too. I'm surprised there is no 2d drawing control specifically for this objective. – Gray-M Aug 16 '12 at 22:05