Tired with the usual gray in windows forms? Yeah, me too! With .Net you can easily create visually beautiful color gradients on your windows forms, and with very minimum code.
Here’s a piece of code that does just that for you. This method takes the top color, bottom color and the object that called the method, it then creates a brush and an area to paint and then draws a gradient on your object which could be a form, a panel etc with a mix of the colors you selected.
Public Sub DrawGradient(ByVal UpperColor As Color, ByVal LowerColor As Color, _
ByVal Caller As Object)
Dim cur As Control = CType(Caller, Control)
Dim objBrush As New Drawing2D.LinearGradientBrush _
(cur.DisplayRectangle, UpperColor, LowerColor, Drawing2D.LinearGradientMode.Vertical)
Dim objGraphics As Graphics = cur.CreateGraphics()
objGraphics.FillRectangle(objBrush, cur.DisplayRectangle)
objBrush.Dispose()
objGraphics.Dispose()
End Sub
The gradient will be vertical, i.e top to bottom, but you can change the LinearGradientMode option to horizontal or diagonal depending on your choice of style.
To call this method, you use the Paint event of a form or a control. As you can see on the code above, I used the DisplayRectangle method of a control to make the code more generic, otherwise, you could use a form, or another control depending on your needs.
I used this code Dim cur As Control = CType(Caller, Control). This way you can call it from a form, a panel control, a button etc. without changing the code.
But if you are going to call it from just forms, then you could change it to this
Dim cur As Form = CType(Caller, Form).
Implementation:
Its easy. Call the method from the object’s Paint event, providing the colors you want to use. I created a simple form called GradientsSample. Here is my implementation on the form’s Paint event.
Private Sub GradientsSample_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
DrawGradient(Color.CornflowerBlue, Color.White, sender)
End Sub
I used the XP style mix of CornflowerBlue and White. See how the form looks like.
See, just beautiful.
I then added a panel to the left side of the form, say you want to use it as your navigation menu. This is how the forms looks like now.
So you see the panel on the left now looks gray and ugly. The form paint event only paints the form and not the controls on the form. Which means we have to use our method to paint the panel control. I added this code to the panel’s Paint event.
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
DrawGradient(Color.DarkSeaGreen, Color.White, sender)
End Sub
For the panel I used the DarkSeaGreen and White colors. The output is just beautiful, as you can see below.

It is really easy to create gradients and make your forms and controls beautiful, instead of using the usual gray that is common and boring.
Till next time, yours truly.


I implemented your example in a test project. Have you noticed what happens if you drag the form beyond the edge of the display? Drag it back into full view and the part of the form that was off the screen will have its original background color. I wonder why that happens. I will probably look around for an event to monitor and draw the gradient again when moving the form happens.
I’m also interested in dealing with background colors of controls such as radio buttons. Their default ugly “control” color really stands out in a bad way against the form. But drawing that same gradient (at least the colors I picked) is not a good solution. Perhaps you have some input about that.
Thanks for the article.