ForrestHeller.com - [:-)]

Auto generating TabIndexes of .NET programs

Navigation
Kitty
Kitty
You
Site
Valid XHTML 1.0 Strict Powered by PHP

About an hour of time is spent each time I release the Alarm Clock of Justice simply getting Alt-shortcut keys and Tab Indexes working as expected. For example, you would expect to tab from left to right, bottom to top--not jump from the top of the screen to the bottom of the screen. I wrote the code below to automatically generate the tab indexes so that I don't have to do it by hand--saving much time.

General Idea

  1. Read in a source file (The code was made for VB.NET but should work in C#)
  2. Recursively walk through all the controls, and compare their relative location on the form (X and Y/Top and Left)
  3. After sorting these controls, give all of them TabIndexes
  4. Sort their children

The code

Here is the code nearly verbatim, with some commented lines removed. It is sloppy, slow, and inefficient. However, as it's not in the final program, I couldn't care less. 'Notes: 'This code is inefficient, but it gets the job done and I really don't care 'I make everything Public by default. Anything else is pointless 'Things that can make this better but I don't care enough to do them: 'This will run faster if replacement_string is a StringBuilder 'The regular expression thing probably brings the system to its knees 'take out the for each loop--it runs slower than a for i = 0 to X 'make it not recursive 'make stuff not passed byval Public const source_file as String = "..\MainForm.vb" Public replacement_string as String Public last_tab_id As Integer = 0 Private Sub A_button_that_gets_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click replacement_string = System.IO.File.ReadAllText(source_file) 'make backup file System.IO.File.WriteAllText(source_file + ".bak, tabstr) traverse_children(Me) System.IO.File.WriteAllText(source_file, replacement_string) End Sub Public Function traverse_children(ByRef mcontrol As Control) As Integer Dim sorted_control_list As New Generic.List(Of Control) Dim i As Integer Dim cnt As Int32 = mcontrol.Controls.Count - 1 For i = 0 To cnt 'Addrange fails? sorted_control_list.Add(mcontrol.Controls(i)) Next sorted_control_list.Sort(AddressOf MainForm.Compare) For Each ncontrol As Control In sorted_control_list If (ncontrol.Name.Length > 0) Then ncontrol.TabIndex = last_tab_id replacement_string = System.Text.RegularExpressions.Regex.Replace(tabstr, ncontrol.Name + ".TabIndex = \w+", ncontrol.Name + ".TabIndex = " + last_tab_id.ToString()) last_tab_id += 1 End If Next 'now traverse these children! for i = 0 to cnt traverse_children(ncontrol.controls(i)) next Return last_tab_id End Function Public Shared Function Compare(ByVal c1 As Control, ByVal c2 As Control) As Integer 'the runtime whines if it passes in the same objects and you say they are different' 'and I always say they are different 'always If (Object.ReferenceEquals(c1, c2)) Then Return 0 End If 'sort by height 'then sort by x '+- 10 difference for things that are relatively close together If (c1.Location.Y > (c2.Location.Y + 10)) Then Return 1 ElseIf (c1.Location.Y < (c2.Location.Y - 10)) Then Return -1 Else If (c1.Location.X > c2.Location.X) Then Return 1 Else Return -1 End If End If Return 0 End Function

Page last modified on July 05 2008 00:01:43 (EDT)