Click here to Skip to main content
15,884,014 members
Articles / Mobile Apps
Article

World's Easiest Way to Reveal Control Names

Rate me:
Please Sign up or sign in to vote.
3.13/5 (5 votes)
19 Apr 20052 min read 26.6K   20   2
Show the name of any control when you mouseover it while debugging.

Introduction

Is that textbox called txtFileName or txtFile? If you're like me, you have a bit of trouble remembering the names of various controls. This easy-to-implement routine will show you the name of any control in the title bar when you mouse over it.

Discussion

One of the first things I do in any project is to implement a way to reveal information to me when I am debugging an app. For instance, many of my error handlers include a call to a special routine that gives me all the dirty details of an error, much of which I want to spare the user from seeing. Another nice debugging extra is to show the name of each control so that I can remember what's what.

To do this, I first enable my custom debug mode. Then, as the app starts up, I run all of the controls through a subroutine that adds a handler to another subroutine that displays the control's name in the title bar.

Enable custom debug mode

The simplest way to do this is with a command line variable. In Visual Studio, click Project, Properties, Configuration Properties and add the command line variable you want. I'll use /d here. Then, add a global Boolean variable isDebug to your code and set it to True if you find the command line variable when you start the project:

VB
Public isDebug as Boolean = False
VB
Public Sub Main
     If Environment.CommandLine.IndexOf("/d")> -1 Then isDebug = True
.....
End Sub

Run controls through a subroutine

The subroutine is recursive--that is, it is likely to call itself. You start the chain of recursive calls by feeding it the name of the biggest control of all--your Form. Subsequent calls are made by the subroutine whenever it finds that the control it is working on has child controls. Here's the subroutine:

VB
Private Sub AddHandlerForDebug(ctrlParent as Control)
  Dim ctrl as Control
  For Each ctrl in ctrlParent.Controls
     AddHandler ctrl.MouseEnter, AddressOf ShowControlName
     If ctrl.HasChildren Then AddHandlerForDebug(ctrl)
  Next
End Sub

Here's the subroutine that a MouseEnter event will trigger:

VB
Private Sub ShowControlName(sender as System.Object, e as System.EventArgs)
     Me.Text = sender.Name
End Sub

Now all that is left is to go back to our Main subroutine and add a call to AddHandlerForDebug:

VB
Public Sub Main
     If Environment.CommandLine.IndexOf("/d")>-1 Then isDebug = True

     If isDebug Then AddHandlerForDebug(Me)
.....
End Sub

You can get as fancy as you want in ShowControlName. For instance, I like to keep the "real" name of my app in the title bar and append the control name in curly braces. And with just a bit more work, you can even have the title bar show you the position of the control, its parent, or any other property.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
Terpy is a consultant reluctantly based in Olympia, Wash. She'd much rather be back in Maine, where the ocean is on the correct side of the road. She is largely a self-taught programmer who clings jealously to her bad habits. Handbells, anyone?

Comments and Discussions

 
GeneralPre-processor Pin
Reiss24-Apr-05 21:28
professionalReiss24-Apr-05 21:28 
GeneralRe: Pre-processor Pin
terpy28-Apr-05 17:02
terpy28-Apr-05 17:02 

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.