C-Sharp Tricks

From TheAlmightyGuru
Jump to: navigation, search

Quick Debug Line

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\Debug.txt", true)) { file.WriteLine("Debug Message"); }

Stacking Transparent Controls

Placing a transparent control over a picture box will not yield expected results. The top control will have a solid colored background based on the BackColor of the Form, even if the control's BackColor is set to Transparent. This is because C# doesn't render form controls from back to front while simply not drawing the transparent sections (the way you would think), but instead draws renders transparent areas with the control's parent, no matter how buried it may be (which is defaulted to the Form).

To correct this problem, you must set the foreground control's parent to the background Picture control and then reposition the foreground control because its new position is relative to the background Picture control. This isn't a perfect fix since you can't draw the foreground object partially over-lapping the background Picture because it will be clipped.

The following code will place a Picture control with a transparent background atop a background Picture control so that the transparency works as expected. Don't forget to set the foreground Picture control's BackColor to transparent.

forePicture.Parent = backPicture;
forePicture.Location = new Point(0, 0);