I have found a solution that works for me. I understood that the real requirement is to have a single code section where I can perform any action required to keep in sync the two objects without creating unnecessary dependencies; the answer is: events. The solution for me has been to create a class (a singleton) where a method has the complete responsibility to load the picture in its PictureBox control; the class has a method to perform the update that fires an event:
/*
* Load the picturebox in the control
*/
public void UpdatePictureBox( Bitmap BitmapToBeLoaded, PictureBox CurrentPictureBox)
{
/*
* If the Bitmap or the picturebox are null there is nothing to update: so exit keeping loaded the previous image
*/
if (BitmapToBeLoaded == null) return ;
CurrentPictureBox.Image = BitmapToBeLoaded; // Assign the Bitmap to the control
/*
* When loading, adapt the control size to the image size to avoid any distortion
*/
CurrentPictureBox.Width = BitmapToBeLoaded.Width; // Adapth width of the control size
CurrentPictureBox.Height = BitmapToBeLoaded.Height; // Adapth height of the control size
/*
* Publish events defensively: Book .Net Components, pag. 108
*/
try
{
if (null != ImageUpdated) ImageUpdated(); // Fire the event
}
catch
{
}
return ;
}
This way all the action to performed after a new image is made available are performed subscribing the event, in a single section of code easily found and documented . I do not claim that this is the best solution; but what makes me happy is the fact that the design seems more clean.