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

World's Easiest Way to Reveal Control Names

, 19 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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:

Public isDebug as Boolean = False
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:

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:

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:

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

About the Author

terpy
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 PinmemberReiss24-Apr-05 21:28 
GeneralRe: Pre-processor Pinmemberterpy28-Apr-05 17:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 19 Apr 2005
Article Copyright 2005 by terpy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid