Click here to Skip to main content
Click here to Skip to main content

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

By , 16 May 2012
 

Introduction

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 two new properties:

  • GlowColor As Color
  • Get or set the color of the glow

  • GlowOn As Boolean
  • Turn the glow effect on or off

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.

Points of interest

Design time view - gGlowBox

Design time view - gGlowGroupBox

OnPaintBackground

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.

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)
        Else
            GlowOn = True
        End If

    End If
End Sub

Private Sub ChildLostFocus()
    GlowOn = False
End Sub

History

  • Version 1.0.0
  • Version 1.0.1
    • Added gGlowGroupBox from Yogi Yang's suggestion.

License

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

About the Author

SSDiver2112
Software Developer
United States United States
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberShahan Ayyub17 Mar '13 - 7:57 
great work!
GeneralMy vote of 5memberXelaFig18 Jan '13 - 7:07 
good development
QuestionWhy Option Strict Off ?memberLogi Guna18 Jan '13 - 4:03 
I think it's better to set Option Strict On in project settings
However, this generate 6 compile errors.
 
I suggest these code to fix 6 compile errors.
* suggestion for gGlowBox.vb
    Private Sub ChildLostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        GlowOn = False
    End Sub
 
    Private Sub ChildGotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
 
        If Controls.Count > 0 Then
            'Check if the control has the ReadOnly property and if so, its value.
            Dim propInfo As Reflection.PropertyInfo = Controls(0).GetType().GetProperty("ReadOnly")
            If propInfo IsNot Nothing Then
                GlowOn = Not DirectCast(propInfo.GetValue(Controls(0), Nothing), Boolean)
            Else
                GlowOn = True
            End If
 
        End If
 
    End Sub
 
take a look at Sub OnPaintBackground(ByVal e As PaintEventArgs) in gGlowBox.vb
I replace these following lines
    'Check if the control has the ReadOnly property and if so, its value.
    If Not IsNothing(_control.GetType().GetProperty("ReadOnly")) Then
        GlowK = Not CallByName(_control, "ReadOnly", CallType.Get)
    End If
with these lines
   'Check if the control has the ReadOnly property and if so, its value.
    Dim propInfo As Reflection.PropertyInfo = _control.GetType().GetProperty("ReadOnly")
    If propInfo IsNot Nothing Then
        GlowK = Not DirectCast(propInfo.GetValue(_control, Nothing), Boolean)
    End If
 
* suggestion for gGlowGroupBox.vb
    Private Sub ChildGotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Invalidate()
    End Sub
 
    Private Sub ChildLostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Invalidate()
    End Sub
 
I think using Reflection.PropertyInfo (implemented in mscorlib.dll) is better and faster than CallByName (implemented in Microsoft.VisualBasic.dll). Please correct me if i wrong.
 
Thanks for this great article! My vote of 4
AnswerRe: Why Option Strict Off ?memberSSDiver211218 Jan '13 - 15:13 
Logi,
 
Sorry about the Option Strict being off. I usually do use it and I just didn't realize I left it off. Thanks for the suggestions I will check them out.
 
SSDiver2112
GeneralMy vote of 5membergeekbond19 Oct '12 - 4:42 
The IExtender property would make this really great, but still my 5.
Question5 stars of course.membermagefesa24 Sep '12 - 9:55 
Dear sir, amazing articles, specially gAnnotation (i ll vote for 5 after write this). One question with gGlowBox... I m trying it on VB.NEt project on Visual studio 2010 and 2012 and works perfectly. But if I try to add the Dll on a VB.Express 2008 project, the DLL does not work on gGlowGROUPBOX, but perfect with GBlowBox WHY ? ? ? ?Any ideas ???
QuestionCould be alot bettermemberi0021 Jun '12 - 20: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...

Kris
AnswerRe: Could be alot bettermemberSSDiver211226 Jun '12 - 16:20 
Thanks for the suggestion, but this has already been suggested below. As I explained I have not dug into the details of the IExtenderProvider yet. This was just an idea I came up with for a project I was working on and it work very well. Of course the IExtenderProvider will offer a greater versatility, but for now I am very happy with how it works and the simplicity of its design.
 
SSDiver2112
SuggestionExcellentmemberDImce Ivanoski16 May '12 - 10:20 
It is a excellent portion of code.It would be nice if there was a C# version.
Nemam takov potpis

GeneralMy vote of 5membervquangchung16 May '12 - 3:36 
Very cool

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

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