Monday, March 5, 2012

Tips: C#, Visual Studio 2010 - Get All Control Names, View TabIndex Order.

Here's a neat trick that I have used every-so-often ever since picking up Visual Basic 6 back in the 90's.  Sure, it can be done in other ways, but this way suites me well.  I can keep the notepad file off sitting to the file to refer to it if needed.

Create a Button control on your form, double-click it, and past this code into it:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\myControlNames.txt"))
                foreach (Control myControls in this.Controls)
                    //MessageBox.Show(myControls.Name + " = " + myControls.GetType());
                    file.WriteLine(myControls.Name + " = " + myControls.GetType());

Run your program (F5) and then click the button.
You can then go to C:\myControlNames.txt file in the C drive, which will display this:

btnGetControlNames = System.Windows.Forms.Button
lblProcessed = System.Windows.Forms.Label
txtNumEvents = System.Windows.Forms.TextBox
lblTimeProcessed = System.Windows.Forms.Label
btnReadEvents = System.Windows.Forms.Button
btnExit = System.Windows.Forms.Button
btnCopy = System.Windows.Forms.Button
label5 = System.Windows.Forms.Label
label3 = System.Windows.Forms.Label
label2 = System.Windows.Forms.Label
label1 = System.Windows.Forms.Label
lblIndex = System.Windows.Forms.Label
lbMessage = System.Windows.Forms.ListBox
lbSource = System.Windows.Forms.ListBox
lbTimeGenerated = System.Windows.Forms.ListBox
lbEntryType = System.Windows.Forms.ListBox
lbIndex = System.Windows.Forms.ListBox
So when running that, it will give you a list of all controls in your form! Cool, eh?

Another tip is this:
If you want to see the TabIndex values for all controls on the form, you can go into Design view (so you are looking at your form), and go to the "View" menu and click on "TabOrder".  It will show all the TabIndex values for each control on your form (if it has them; things like PictureBoxes do not).  Use the "Escape" button to exit out of this mode (or use the "View" menu again).  Using the mouse to left-click on different objects can manipulate the TabIndexes.

Have fun!

No comments:

Post a Comment