Introduction
Sometimes you are working with lots of form controls and some of them may bleed over other controls. I have found this to be a problem when the opacity is less than 100% (not opaque). For example, a dropdown control may drop into a textbox control and leave a trail behind. Look at the screen shot below. I am not sure if I would call this particular issue a bug; but, as the ultimate perfectionist that I am, it could not go ignored.
This small piece was written to work around that annoying problem.
Solving This Issue
The trick to solving this problem is invalidating the region
that the control may have occupied. In this case, the region
is the textbox(es) under our combobox. To do that, we use the Control.Invalidate(region)
method. If you decide not to specify a region
for the method (i.e. this._textbox1.Region
), the entire client area - that the control occupies - will be redrawn. If your controls are inside a container (i.e. a panel control); you may choose to call the invalidate
method for the panel itself.
The Invalidate
method sends an asynchronous request to the form's owner thread telling it to update (raise the paint event for the control and children if applicable) when it has an opportunity. Fortunately for us, the delay is hardly noticeable. In fact, it usually is not noticeable at all. If you want to completely take over painting events, you could use the Control.Update()
method. This makes a synchronous call to the thread: raising the paint event right away. I don't think this is recommended though, because paint operation may be time consuming and the UI thread is unavailable until the thread gets done painting the client area. You may already know, nobody likes an unresponsive GUI.
This issue also happens - to me at least - under the 2.0 version of the Framework. The technique outlined works just as well with the 2.0 Framework and Visual Studio 2005.
Tip
The quickest way to view and run the code download is using Snippet Compiler.
History
- 17th May, 2006: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.