Click here to Skip to main content
13,590,480 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 25 Apr 2012
Licenced CPOL

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

, 16 May 2012
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

QuestionNon .NET apps Pin
august21323-Mar-17 9:08
memberaugust21323-Mar-17 9:08 
QuestionDrop shadow effect Pin
N. Henrik Lauridsen27-May-15 23:06
memberN. Henrik Lauridsen27-May-15 23:06 
AnswerRe: Drop shadow effect Pin
SSDiver211229-May-15 12:00
memberSSDiver211229-May-15 12:00 
GeneralRe: Drop shadow effect Pin
N. Henrik Lauridsen29-May-15 22:30
memberN. Henrik Lauridsen29-May-15 22:30 
GeneralMy vote of 5 Pin
Shahan Ayyub17-Mar-13 7:57
memberShahan Ayyub17-Mar-13 7:57 
GeneralMy vote of 5 Pin
XelaFig18-Jan-13 7:07
memberXelaFig18-Jan-13 7:07 
QuestionWhy Option Strict Off ? Pin
Logi Guna18-Jan-13 4:03
memberLogi Guna18-Jan-13 4:03 
AnswerRe: Why Option Strict Off ? Pin
SSDiver211218-Jan-13 15:13
memberSSDiver211218-Jan-13 15:13 
GeneralMy vote of 5 Pin
geekbond19-Oct-12 4:42
membergeekbond19-Oct-12 4:42 
Question5 stars of course. Pin
magefesa24-Sep-12 9:55
membermagefesa24-Sep-12 9:55 
QuestionCould be alot better Pin
i0021-Jun-12 20:48
memberi0021-Jun-12 20:48 
AnswerRe: Could be alot better Pin
SSDiver211226-Jun-12 16:20
memberSSDiver211226-Jun-12 16:20 
SuggestionExcellent Pin
DImce Ivanoski16-May-12 10:20
memberDImce Ivanoski16-May-12 10:20 
GeneralMy vote of 5 Pin
vquangchung16-May-12 3:36
membervquangchung16-May-12 3:36 
GeneralRe: My vote of 5 Pin
SSDiver211216-May-12 3:38
memberSSDiver211216-May-12 3:38 
QuestionDoesnt Work.. Pin
fuujinn15-May-12 21:45
memberfuujinn15-May-12 21:45 
AnswerRe: Doesnt Work.. Pin
SSDiver211216-May-12 3:18
memberSSDiver211216-May-12 3:18 
AnswerRe: Doesnt Work.. Pin
SSDiver211216-May-12 3:38
memberSSDiver211216-May-12 3:38 
GeneralMy vote of 5 Pin
CarstenV213-May-12 20:12
memberCarstenV213-May-12 20:12 
GeneralMy vote of 5 Pin
Monjurul Habib10-May-12 19:26
memberMonjurul Habib10-May-12 19:26 
GeneralMy vote of 3 Pin
Clark Kent1238-May-12 2:05
memberClark Kent1238-May-12 2:05 
GeneralRe: My vote of 3 Pin
SSDiver21128-May-12 9:07
memberSSDiver21128-May-12 9:07 
GeneralRe: My vote of 3 Pin
Clark Kent1238-May-12 9:17
memberClark Kent1238-May-12 9:17 
Questionvery cool Pin
CIDev7-May-12 11:01
memberCIDev7-May-12 11:01 
GeneralMy vote of 5 Pin
Gregory.Gadow7-May-12 10:48
memberGregory.Gadow7-May-12 10:48 

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.

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