Post

How to cause Visual Studio .NET run out of memory ;-)

I’m attempting to create a re-usable version of my semi-transparent panel control (TransPanel).  Not much of a problem, all of the GDI+ code has been created previously.  Of course, I’m also creating a semi-transparent label control (TransLabel) to complement this control.  The trick is that I am using the background image (if one exists) for the parent of the transparent control (either the label or panel) to determine what the base image should be before overlaying the semi-transparent panel/label background over the base image (otherwise, I create a base image using the parents backcolor). 

Fine, this works great for the components placed on the main form.  But what happens when I want to place a TransLabel inside of a TransPanel.  Well, since I was using the OnPaint event to draw the TransPanel controls display, the BackgroundImage property isn’t being set for use by the contained TransLabel.  OK, I thought (well, not really much thinking)… I’d just set the BackgroundImage to the bitmap that I’m creating during the OnPaint.  Ooops… it became apparent right away that wouldn’t work.  After Visual Studio .NET locked up on me for about 45 seconds, I got a nice little message box telling me I ran out of memory.  Of course it did.  Setting the BackgroundImage caused a repaint to occur. 

At this point, I thought I’d use SuspendLayout and ResumeLayout.  Nope, that didn’t work either.  Same results.

So what to do to fix this?  How about not using the OnPaint event?  Since I’m causing an invalidate to occur on a couple of events (parents background image changing, move, resize, etc.), I’d just create a method to change the background image accordingly.  Works like a champ, problem solved. ;-)

This post is licensed under CC BY 4.0 by the author.