Is that textbox called
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.
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
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
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)
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
Now all that is left is to go back to our
Main subroutine and add a call to
Public Sub Main
If Environment.CommandLine.IndexOf("/d")>-1 Then isDebug = True
If isDebug Then AddHandlerForDebug(Me)
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.