1

I have a PictureBox which resides in Class A (essentially a single tabpage), which has all of its methods OnPaint() defined inside there.

I want to declare another PictureBox2 which resides in Class B (another tabpage). So technically all of its OnPaint() code will be the same, just that the name changes.

My question is that which would be a better way: To copy the entire OnPaint() in Class A into Class B, modifying the PaintBox that it refers to; or is there a better way to avoid the double work? (Like calling the Class A OnPaint() from Class B with a parameter?)

John Tan
  • 1,331
  • 1
  • 19
  • 35
  • You might also consider repackaging your code into a [`CustomControl` or `UserControl`](http://stackoverflow.com/questions/1322451/what-is-the-difference-between-user-control-custom-control-and-component), where you can reuse more than just the `OnPaint` handler – StuartLC May 25 '15 at 04:50
  • Would inheriting from the same base class solve your challenge? Or a static method that resides somewhere? – Quality Catalyst May 25 '15 at 04:51
  • 1
    There is not enough context here to answer the question. For example, why do you want a second class? What will be different about the two `PictureBox` subclasses? For that matter, why are you subclassing `PictureBox` if you're just overriding `OnPaint()`? Why not just subscribe to the `Paint` event? If you really need two different subclasses, and you just want to share the code in `OnPaint()`, why not just create a helper class that contains that code, and call it from each subclass? – Peter Duniho May 25 '15 at 05:21
  • I think Peter is right: create the painting method in a 3rd class and pass in the sendr/picturebox and the e.Graphics from the Paint events of both classes PBs'. If needed you can add more params.. – TaW May 25 '15 at 09:25

0 Answers0