Click here to Skip to main content
15,885,116 members
Articles / Programming Languages / C#
Article

Preventing Form Controls Bleeding

Rate me:
Please Sign up or sign in to vote.
2.44/5 (3 votes)
17 May 2006CPOL2 min read 24.9K   115   6   1
Preventing control bleeding in Windows Forms
Sample Image - PreventingControlBleed.jpg

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Toli Cuturicu19-Oct-10 2:13
Toli Cuturicu19-Oct-10 2:13 
This should be a tip/trick, not a full article.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.