Visual Studio Tab Un-stupidifier Macro

Necessity is the mother of dodgy macros, they say. Here's one I made to fix the stupid most recently- to last recently-used document tab order of Visual Studio. But before the code, here is my harrowing tale....

I remember doing user interface design courses in my undergrad study, and thinking "I don't need to pay attention to this - by the time I'm out in the real world UI standards will be firmly in place."

I remember this today as I try to tab to an open document in Visual Studio.

I can see the tab - it is right in front of me, but ctrl-tab as I might I can not get to it. The active document jumps around from document to document until finally, like a really really frustrating game of roulette, I land on the code page I seek. Seeked. Sake?

By this time I had forgotten why I was even going to the page, and realised that every time I hit ctrl-tab (or ctrl-f6 in VS 2005), I ground my teeth just a little bit more.

Don't get me wrong, being able to go to the last file you used is pretty handy. It's just not as handy as being able to scroll through your open documents without having to yell rude words at your co-workers.

mru vs normal tab orderIf fact, the MRU order is good if you only ever need to access the last file you were working on. Which is unlikely to be the case if you are working on a chunky project with lots of documents open. See my graph for clarification.

So I had a look around for the necessary keyboard short-cut, or registry setting I needed to apply to remove someone's great idea for changing the way tabs work. I couldn't find it, so in a fit of angriness, I ended up writing this dodgy macro. It's probably THE worst way to accomplish the task, but hey - I use a GOTO statement, so it can't be all bad:

Public Sub TabForward()
	Dim i As Integer
	Dim activateNext As Boolean = False

	For i = 1 To DTE.Windows.Count
		If DTE.Windows().Item(i).Kind = "Document" Then

			If activateNext Then
				DTE.Windows().Item(i).Activate()
				GoTo done
			End If

			If DTE.Windows().Item(i) Is DTE.ActiveWindow Then
				activateNext = True
			End If
		End If
	Next

	' Was the last window... go back to the first
	If activateNext Then
		For i = 1 To DTE.Windows.Count
			If DTE.Windows().Item(i).Kind = "Document" Then
				DTE.Windows().Item(i).Activate()
				GoTo done
			End If
		Next
	End If
done:

End Sub

Public Sub TabBackward()
	Dim i As Integer
	Dim activateNext As Boolean = False

	For i = DTE.Windows.Count To 1 Step -1
		If DTE.Windows().Item(i).Kind = "Document" Then

			If activateNext Then
				DTE.Windows().Item(i).Activate()
				GoTo done
			End If

			If DTE.Windows().Item(i) Is DTE.ActiveWindow Then
				activateNext = True
			End If
		End If
	Next

	' Was the first window... go back to the last
	If activateNext Then
		For i = DTE.Windows.Count To 1 Step -1
			If DTE.Windows().Item(i).Kind = "Document" Then
				DTE.Windows().Item(i).Activate()
				GoTo done
			End If
		Next
	End If
done:

End Sub

I don't know if it works in VS2005 - I only have the freebie version.

All that's left to do then is to map TabForward to ctrl-tab and TabBackward to ctrl-shift-tab. You do this in tools->options->keyboard. I then set the old Window.NextDocumentWindow to ctrl-f6 so if I want to use the MRU order I can.

So far I haven't wanted to.