Click here to Skip to main content
11,922,641 members (32,707 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked

gGlowBox - Create a glow or shadow effect around a focused control (VB.NET)

, 16 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A custom Panel that creates a glow effect around a child control or a drop shadow when it receives focus.



I really like the glow effect around a focused Textbox when inputting data on a form on a Chrome web page. It really makes it easier to track and enter data. I wanted to have this same effect on a Winform. I thought, here we go again with another big control project, but to my surprise I got the basic control working in no time with very little code.

Using the code

The gGlowBox Inherits a Panel, resizes to fit one child control (any control, not just a Textbox), and activates and deactivates the glow effect when the child control gets or loses focus.

There are only three new properties:

Get or set the color of the glow

Turn the glow effect on or off

  • EffectType As eEffectType
  • GlowColor As Color
  • GlowOn As Boolean

Simple to use: Drop the gGlowBox on a Form, set the GlowColor, drop a control in the gGlowBox (the gGlowBox will resize automatically to fit the child control), and resize if needed.

In version 1.0.1, from Yogi Yang's suggestion, I added a new control that can handle a group of controls. I added the gGlowGroupBox rather than replace the original control to allow more flexibility. The main coding difference is the gGlowGroupBox has the re-sizing methods removed.

In version 1.0.2, from N. Henrik Lauridsen's suggestion, I added a Drop Shadow option to the gGlowGroupBox.

Points of interest

Design time view - gGlowBox

Design time view - gGlowGroupBox


Using a technique explained in the gLabel[^] control, a blurred rectangle is drawn around the edge of the gGlowBox. Because the gGlowBox is slightly bigger than the child control, it creates a glowing effect around the child control. For the DropShadow the Blurred Rectangle is drawn at an offset.

Sizing the gGlowBox and the child control

To make sizing easier, the gGlowBox sizes itself automatically to the child control.

First if the child control is Added or Resized, the Layout event fires and resizes the gGlowBox to match up with the child control.

Private Sub gGlowBox_Layout(ByVal sender As Object, _
        ByVal e As LayoutEventArgs) Handles Me.Layout

    'Resize the gGlowBox to fit in the Child Control size
    If Controls.Count > 0 Then
        If e.AffectedControl Is Controls(0) Then
            Size = New Size(Controls(0).Width + 7, Controls(0).Height + 7)
            Controls(0).Location = New Point(4, 4)
        End If

    End If

End Sub

Second, when the gGlowBox is resized, the Resize event will size the child control to fit inside the gGlowBox. An anchored gGlowBox will not return to its original size when the Parent form is minimised unless this step is skipped, so a check is made to see what the Form’s WindoState is first.

Private Sub gGlowBox_Resize(ByVal sender As Object, 
       ByVal e As System.EventArgs) Handles Me.Resize

    'This is needed to avoid resizing an Anchored gGlowBox 
    ' when the parent Form is Minimized 
    If IsNothing(FindForm) OrElse 
       FindForm.WindowState = FormWindowState.Minimized Then Exit Sub

    'Resize the Child Control to fit the size of the gGlowBox
    If Controls.Count > 0 Then
        Controls(0).Size = New Size(Width - 7, Height - 7)
    End If

End Sub
Control focus event wire-up

The child control’s GotFocus and LostFocus events are added to the gGlowBox in the ControlAdded event and point to the ChildGotFocus and ChildLostFocus methods. These methods simply turn the glow on and off.

Private Sub gGlowBox_ControlAdded(ByVal sender As Object,_
       ByVal e As ControlEventArgs) Handles Me.ControlAdded
   ' Add handlers to let the gGlowBox know
   ' when the child control gets Focus
   AddHandler e.Control.GotFocus, AddressOf ChildGotFocus
   AddHandler e.Control.LostFocus, AddressOf ChildLostFocus
End Sub

As an extra note, I did not want the glow to turn on if the Textbox was set to ReadOnly. Simple if the child control was always going to be a Textbox, but the gGlowBox was designed to house any basic control. Some controls like the Combobox do not have this property. To deal with this, I used the GetProperty function to check if the unknown control has that property and if it does, use the CallByName method to get the value.

Private Sub ChildGotFocus()

    If Controls.Count > 0 Then
        'Check if the control has the ReadOnly 
        'property and if so, its value.
        If Not IsNothing(Controls(0).GetType().GetProperty("ReadOnly")) Then
            GlowOn = Not CallByName(Controls(0), "ReadOnly", CallType.Get)
            GlowOn = True
        End If

    End If
End Sub

Private Sub ChildLostFocus()
    GlowOn = False
End Sub


  • Version 1.0.0
  • Version 1.0.1
    • Added gGlowGroupBox from Yogi Yang's suggestion.
  • Version 1.0.2
    • Added DropShadow Option to the gGlowGroupBox from N. Henrik Lauridsen's suggestion.


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


About the Author

Software Developer
United States United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba buisness during this time. Currently I am a Database manager and software developer. I started with VBA and VB6 and now having fun with VB.NET/WPF/C#...

You may also be interested in...

Comments and Discussions

QuestionDrop shadow effect Pin
N. Henrik Lauridsen28-May-15 0:06
memberN. Henrik Lauridsen28-May-15 0:06 
AnswerRe: Drop shadow effect Pin
SSDiver211229-May-15 13:00
memberSSDiver211229-May-15 13:00 
GeneralRe: Drop shadow effect Pin
N. Henrik Lauridsen29-May-15 23:30
memberN. Henrik Lauridsen29-May-15 23:30 
GeneralMy vote of 5 Pin
Shahan Ayyub17-Mar-13 8:57
memberShahan Ayyub17-Mar-13 8:57 
GeneralMy vote of 5 Pin
XelaFig18-Jan-13 8:07
memberXelaFig18-Jan-13 8:07 
QuestionWhy Option Strict Off ? Pin
Logi Guna18-Jan-13 5:03
memberLogi Guna18-Jan-13 5:03 
AnswerRe: Why Option Strict Off ? Pin
SSDiver211218-Jan-13 16:13
memberSSDiver211218-Jan-13 16:13 
GeneralMy vote of 5 Pin
geekbond19-Oct-12 5:42
membergeekbond19-Oct-12 5:42 
Question5 stars of course. Pin
magefesa24-Sep-12 10:55
membermagefesa24-Sep-12 10:55 
QuestionCould be alot better Pin
i0021-Jun-12 21:48
memberi0021-Jun-12 21:48 
I think the implementation is poorly done..

A better way (for eg) would be to put all of your controls on something that implements IExtenderProvider so that you can place your controls on it, and have a property Eg AutoHighlight that when set to true on those child controls draws the glow effect.

Just my two cents...

AnswerRe: Could be alot better Pin
SSDiver211226-Jun-12 17:20
memberSSDiver211226-Jun-12 17:20 
SuggestionExcellent Pin
DImce Ivanoski16-May-12 11:20
memberDImce Ivanoski16-May-12 11:20 
GeneralMy vote of 5 Pin
vquangchung16-May-12 4:36
membervquangchung16-May-12 4:36 
GeneralRe: My vote of 5 Pin
SSDiver211216-May-12 4:38
memberSSDiver211216-May-12 4:38 
QuestionDoesnt Work.. Pin
fuujinn15-May-12 22:45
memberfuujinn15-May-12 22:45 
AnswerRe: Doesnt Work.. Pin
SSDiver211216-May-12 4:18
memberSSDiver211216-May-12 4:18 
AnswerRe: Doesnt Work.. Pin
SSDiver211216-May-12 4:38
memberSSDiver211216-May-12 4:38 
GeneralMy vote of 5 Pin
CarstenV213-May-12 21:12
memberCarstenV213-May-12 21:12 
GeneralMy vote of 5 Pin
Monjurul Habib10-May-12 20:26
memberMonjurul Habib10-May-12 20:26 
GeneralMy vote of 3 Pin
Clark Kent1238-May-12 3:05
memberClark Kent1238-May-12 3:05 
GeneralRe: My vote of 3 Pin
SSDiver21128-May-12 10:07
memberSSDiver21128-May-12 10:07 
GeneralRe: My vote of 3 Pin
Clark Kent1238-May-12 10:17
memberClark Kent1238-May-12 10:17 
Questionvery cool Pin
CIDev7-May-12 12:01
memberCIDev7-May-12 12:01 
GeneralMy vote of 5 Pin
Gregory.Gadow7-May-12 11:48
memberGregory.Gadow7-May-12 11:48 
QuestionFive stars, of course. Pin
xry200630-Apr-12 4:23
memberxry200630-Apr-12 4:23 
GeneralMy vote of 5 Pin
Jαved30-Apr-12 0:15
memberJαved30-Apr-12 0:15 
GeneralAnother tool for the toolbox Pin
rspercy6526-Apr-12 1:16
memberrspercy6526-Apr-12 1:16 
GeneralMy vote of 5 Pin
Yogi Yang25-Apr-12 18:57
memberYogi Yang25-Apr-12 18:57 
QuestionReally very Cool Pin
Yogi Yang25-Apr-12 18:55
memberYogi Yang25-Apr-12 18:55 
AnswerRe: Really very Cool Pin
SSDiver211225-Apr-12 19:29
memberSSDiver211225-Apr-12 19:29 
GeneralRe: Really very Cool Pin
Zac Greve26-Apr-12 3:22
memberZac Greve26-Apr-12 3:22 
GeneralRe: Really very Cool Pin
SSDiver211226-Apr-12 9:09
memberSSDiver211226-Apr-12 9:09 
GeneralRe: Really very Cool Pin
Zac Greve28-Apr-12 7:11
memberZac Greve28-Apr-12 7:11 
GeneralCool Pin
Zac Greve25-Apr-12 11:01
memberZac Greve25-Apr-12 11:01 
GeneralRe: Cool Pin
SSDiver211225-Apr-12 11:15
memberSSDiver211225-Apr-12 11:15 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.1 | Last Updated 16 May 2012
Article Copyright 2012 by SSDiver2112
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid