Adsense

Tuesday, September 18, 2007

Visual Studio - Item Template Editing (Class, WPF, Forms, ETC...)

I've been working in Visual Studio for a few years now, and have created hundreds of classes, user controls, windows, forms, pages... You get the picture. I'm also the type of developer that likes to keep my code organized and generally start off with a couple of standard code regions (Fields, Properties, Events, etc...) in my classes and code-behind files.

I always imagined that there would be a simple way to override the default templates for these files to make my job easier, and today I finally spent the time to track it down...

Visual Studio Item Templates are located in the following directories...

Item Template Files

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates

Item Template Cache Files

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache

Within each of these directories you'll have folders for CSharp, VisualBasic, etc. These folders hold all of the Item Templates for the named language. To edit the base implementation of the Class.cs file, you would browse to "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip" and load up the Class.cs file. This file would look something like this by default.

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
        public $safeitemrootname$()
        {
        }
    }
}

You might end up modifying this file like so to add a few regions by default...

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
        #region Fields & Properties
        #endregion

        #region Constructors
        public $safeitemrootname$()
        {
        }
        #endregion

        #region Private Methods
        #endregion

        #region Public Methods
        #endregion
    }
}

The next time you add a Class.cs file to one of your projects, it will come complete with the regions that you added to the template. You can also customize these templates further using other template parameters (i.e. '$clrversion$' for the Current version of the common language runtime (CLR).)

You can find a list of template parameters HERE....

Friday, September 07, 2007

Windows Vista - Window Focus - Alt + Tab Feature Tweaks

One of my favorite Vista features is their implementation of the Alt + Tab hotkey for switching window focus. Unlike Windows XP which would allow you to press Alt + Tab to cycle through your open windows. Windows Vista allows you to hold in Alt + Tab and then click on one of the displayed thumbnails to change focus.

Such a simple change, but with how often I use the hotkey to switch window focus. I find it much easier to hit the hotkey and quickly click on the window I want rather than cycling. My only problem with the feature is how small the icons are, especially on high resolution monitors. I found this great post which explains how to tweak this feature with a registry entry to make the displayed thumbnails any size you wish.

Save the following snippet to a text file with a .reg extension. Double-click the file you created to update your registry. This is optimized for my laptop's resolution of 1440X900, but feel free to tweak the settings for your resolution. You can tweak the size of each thumbnail, and the spacing between them...

(TIP:  Use a Hexadecimal Converter to get your values right. Like This One)

#### Snippet ####

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AltTab]
"OverlayIconPx"=dword:00000023
"OverlayIconDXPx"=dword:00000000
"OverlayIconDYPx"=dword:00000000
"ThumbSpacingXPx"=dword:00000004
"ThumbSpacingYPx"=dword:00000004
"SideMarginPx"=dword:00000004
"BottomMarginPx"=dword:0000000a
"MinThumbSizePcent"=dword:00000064
"MinWidthPx"=dword:0000012c
"TopMarginPx"=dword:00000020
"MaxThumbSizePx"=dword:000000c8
"MaxIconSizePx"=dword:000000fa
"TextBottomPx"=dword:000001f4
####/Snippet####

To revert back to the original settings, remove the added key. "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AltTab"


I also use a Logitech mouse that has 6 buttons. The mouse comes with it's own Document Switch feature, but I hate how they implemented it. A comment on the post I mention above recommended assigning this hotkey to a mouse button to make changing window focus even easier. On my Logitech mouse, I replaced their document switch button with the Ctrl + Alt + Tab hotkey (as recommended) which keeps the Alt+Tab selection window open indefinitely, so that you can click a thumbnail at your leisure... Very handy!


I am now using both of these tweaks and switching windows focus has never been easier, faster, or more intuitive. You should try it out.

Thursday, September 06, 2007

WPF - Error 3 API restriction: The assembly 'file:///C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStu

I was getting any annoying error (see below) in Visual Studio 2008 (Team System) Beta II after doing an SVN update. Every time I would try to compile our solution, I would get an error in our UnitTest project. The error was complaining that the UnitTestFramework.dll was being loaded from two different locations within the same appdomain.

"Error 3 API restriction: The assembly 'file:///C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain."

The problem was that two of the projects in our solution were referencing UnitTesting DLLs. For some reason someone added references to the following two libraries in our DAL project, and the compiler was throwing a fit that the UnitTestFramework dll was getting loaded from two different locations within the appdomain.

  • Microsoft.VisualStudio.QualityTools.UnitTestFramework
  • Microsoft.VisualStudio.TeamSystem.Data.UnitTesting

Removing these two (unneeded) references from our DAL project fixed the issue, but the error seemed a bit vague.

(NOTE: You may need to close and then reopen Visual Studio since the library might still be loaded...) =( Took me 10 minutes to figure this out...)