Click here to Skip to main content
Click here to Skip to main content

Adding Guidelines to Visual Studio

By , 14 Aug 2006
 

Sample Image - Guidelines.png

Introduction

When I logged on to CodeProject this morning, the weekly poll asked the question, "How wide is your source code?". This led to a discussion in the forums, where Vikram posted a link to this article by Sara Ford:

The article details a little hack that you can make to the registry so that one or more "guidelines" can be dispalyed in Visual Studio. (See the image above for an example of what this looks like in the editor.) So the bottom line is, the end result of this article isn't really due to my own bright idea - the only original part of this is in creating a tool to perform the registry hack for you, with minimal effort on your part.

I won't bother to rehash the content of the article here, as you can read it freely from Sara's blog. I will simply address the tool, and what went into it.

The Tool

The tool is so simple it's almost silly to review it. It took me about 10 minutes to hack together from start to finish. (Update: with all the updates listed below, I'd say there is roughly five hours of work in this now.) Basically, it allows you to select a color to set the guidelines to, and to enter a list of column addresses to place the guidelines at. Click "Apply" and the changes are made. Click "Remove" and the changes are removed. It's a no brainer.

The changes are made through some simple calls to the registry.

private void btnApply_Click(object sender, EventArgs e)
{
    if (txtPreview.Text != "" && txtLocations.Text != "")
    {
        // Create a new key 
        RegistryKey key = 
           Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\" + 
           "VisualStudio\\8.0\\Text Editor", true);
        // Set value of sub key
        key.SetValue("Guides", txtPreview.Text);
	key.Close();
    }
}
private void btnRemove_Click(object sender, EventArgs e)
{
    RegistryKey delKey = 
         Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\" + 
         "VisualStudio\\8.0\\Text Editor", true);
    delKey.DeleteValue("Guides");
    dekKey.Close();
}

The IsNumeric Problem

C# is a wonderful language, but every now and then, you wander into a situation that leaves you scratching your head in disbelief. One of the things I wanted to do was to make sure that the list of guideline locations was a comma seperated list of integers. My first thought (being an old VB programmer at heart) was to simply Split() the string and check to see if each value was a number by asking "IsNumeric()"? But lo and behold, C# has no such function! The Char type has a similar function, but not String.

My first attempt (and you might have downloaded this copy, see the update below) was to Split() the string, and then "borrow" the function by stealing from VB, like this:

private bool IsNumeric(string value)
{
    return Microsoft.VisualBasic.Information.IsNumeric(value);
}

That worked just fine for the most part, then I realized that you could still enter a value such a -9, or 9.5, and it would pass muster. While those values probably wouldn't hurt anything, they weren't helping it any either, so I came up with a new plan, and the one I should have gone with in the first place, using a regular expression to validate the input. Not only did this minimize a lot of code in my program, but it works better in the end.

private bool verifyLocations(string locations)
{
    Regex regex = new Regex("^(\\d|,)*\\d*$");
    return regex.IsMatch(locations.Replace(" ", ""));
}

Update (Aug 14, 2006)

This article and code has been updated since the initial release. The following features and changes have been implemented:

  • Error checking is now included
  • The preview textbox is readonly
  • The preview textbox now correctly updates when you change the guideline locations
  • The registry is now explicitly closed after each transaction, as reccomended in the comments below
  • A help file is included
  • Settings are persisted between sessions
  • Error checking is now via Regex, much less coding and no VB reference required
  • Program now reads in value from registry as requested in comments below
  • More comments added to the source code
  • Now handles VS 2003 & VS 2005
  • Help greatly expanded
  • UI changed to reflect new features
  • Error handling now uses the ErrorProvider model and is handled more appropriately
  • Some minor spelling errors corrected
  • Reduced the size of the source file
  • Added tab stops
  • Max. # of guidelines is now enforced
  • Bugfix: Preview color now displays correctly when loading from registry
  • Bugfixes: load()
  • Bugfixes: several required null checks 

Known Problems

The code is pretty solid now. The only known "bug" is that I don't currently check for maximum column values - you could enter 20,000 as a column, and unless you have a monitor that wraps around the entire room, you'd never see it. This being a developer tool, I felt it was uneccessary to spell this out.

Special Thanks

To all the people who made suggestions for this tool, especially to Gordon Brandly who suggested some of the UI updates and provided the code to make this work with VS2003. Also, thanks to those of you who put in a 5 vote for this article, I really appreciate it.

Disclaimer

This tool is free and open source, so do what you want with it. Giving me some credit somewhere, for it would be nice, but not required. It does make changes to your registry, so backup your registry before using it! I take no resonsibility for any damage caused to your system by this tool.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Todd Davis
Systems Engineer Virtual RadioLogic
United States United States
Member
Todd Davis has been working in web and application development for several years, using Silverlight, ASP.NET, VB.NET, C#, C++ and Javascript, as well as a great deal of work with SQL server and IIS.
 
He currently works for Virtual Radiologic in Eden Prairie, MN, however he is better known for his varied work in the open source community, especially the DotNetNuke project for which he provided several world-renowned training videos and modules. A huge advocate of open source and open knowledge sharing, everything on his website (www.SeaburyDesign.com) is always offered for free.
 
Whenever he is not actively coding at his laptop (a rarity to be sure), he can be found woodworking, walking with his wife and kids, or motoring along the back roads of MN on his Harley Davidson Fatboy.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Bug or feature?memberTodd Davis14 Aug '06 - 13:41 
Thanks - actually, upon review I found even more bugs than that. load() was accepting a path parameter but not actually making use of it, and several functions were obtaining a key but not checking for null value, which was causing an error when the key didn't exist.
 
I simply must stop working so much - I'm losing my edge.
 
-Todd Davis (toddhd@gmail.com)

GeneralThank youadminChris Maunder10 Aug '06 - 0:05 
Why this isn't in the VS options I'll never know...
 
cheers,
Chris Maunder
CodeProject.com : C++ MVP

GeneralRe: Thank youmemberTodd Davis10 Aug '06 - 2:36 
Me either, as it seems like people want it. After I get this ironed out, I might try and turn this into a VS plugin for convenience sake, although I think most people will tend to run this once and then forget about it afterwards.
 
-Todd Davis (toddhd@gmail.com)

GeneralRead existing registry values [modified]memberOlaf Herrmann9 Aug '06 - 22:01 
Please read the the existing value from the registry when loading the main form.
 
Thanks
 

-- modified at 4:01 Thursday 10th August, 2006
GeneralRe: Read existing registry valuesmemberTodd Davis10 Aug '06 - 2:34 
Ah, right! I knew there was something I forgot. Blame it on coding while watching reruns of "The 4400", a poor mix to be sure. I will add the feature in the next 24 hours and credit you in the comments Olaf. Thanks for the input.
 
-Todd Davis (toddhd@gmail.com)

QuestionDotted lines for function body ??memberLukky9 Aug '06 - 21:56 
Hi,
 
Looking at your screenshot, I noticed that you have dotted line delimiting function bodies.
 
I tried finding the setting to get that, but can't find it.
 
Where is this option set.
 
Thank you.
 
Luc Morin

AnswerRe: Dotted lines for function body ??memberTodd Davis10 Aug '06 - 2:31 
There are a few unique things in my IDE if you look carefully. I love that function body delimter, and I don't mind giving a free plug here for the product...
 
Those delimters, and a host of other incredibly productive tools, are from a package called CodeRush (along with Refactor Pro!) made by DevExpress. Here is the direct link:
 
http://www.devexpress.com/Products/NET/IDETools/CodeRush/[^]
 
I swear by these tools, and cannot reccomend them enough.
 
-Todd Davis (toddhd@gmail.com)

GeneralUpdatesmemberTodd Davis9 Aug '06 - 15:06 
Please see the list of updates above. If you have any suggestions or problems, please let me know.
 
-Todd Davis (toddhd@gmail.com)

GeneralGreat little program -- works good with VS 2003 toomemberGordon Brandly9 Aug '06 - 11:16 
Thanks for that very useful little program. Cool | :cool: Not having that guideline is one of the few things I miss when I work in Visual Studio instead of C++Builder.
 
I've modified your program to work with Visual Studio 2003 as well, and it works great. I'd be happy to send you my modified project if you like.
GeneralRe: Great little program -- works good with VS 2003 toomemberTodd Davis9 Aug '06 - 11:25 
Cool. Yes, please send that, I'll add it. Maybe I'll update the error checking as well. Then it won't be half-baked.
 
-Todd Davis (toddhd@gmail.com)

GeneralRe: Great little program -- works good with VS 2003 toomemberGordon Brandly9 Aug '06 - 11:59 
Hmm. I tried sending you the files, but I can't seem to send to your e-mail address from this computer. I'll try again later from another computer.
QuestionHow about calling Close on the registry keys...staffNishant Sivakumar9 Aug '06 - 9:19 
...after you are done modifying/deleting sub-keys/values?
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications.
Also visit the Ultimate Toolbox blog (New)

AnswerRe: How about calling Close on the registry keys...memberTodd Davis9 Aug '06 - 9:32 
That is certainly something I can add in. From my understanding, the registry info flushes and closes when the app closes (at the very least. When I was testing it, the changes were made right away). Since we are dealing with only one key, and the app only does one thing (we aren't making hundreds of registry changes) it didn't seem neccessary to specify this explicitly. But to be safe and/or complete, I'll be happy to add it when/if I make changes to the code.
 
Thank you for the suggestion.
 
-Todd Davis (toddhd@gmail.com)

GeneralRe: How about calling Close on the registry keys...memberJamie Nordmeyer9 Aug '06 - 10:32 
It's still good practice, however, to follow the guidelines of opening system resources as late as possible, and closing them as early as possible, to avoid any issues where the app crashes, leaving unclosed registry keys, allocated GDI objects, etc.
 
Looks like an interesting concept, though. VS Guidelines. I'll have to try it. Smile | :)
 
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA

GeneralRe: How about calling Close on the registry keys...memberTodd Davis9 Aug '06 - 15:22 
This feature has been added, amongst several others.
 
-Todd Davis (toddhd@gmail.com)

AnswerRe: How about calling Close on the registry keys...memberleppie10 Aug '06 - 4:34 
Being a bit pedantic, arent you? Poke tongue | ;-P
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 14 Aug 2006
Article Copyright 2006 by Todd Davis
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid