给Visual Studio添加引导线[翻译]

今天在http://www.codeproject.com/useritems/Guidelines.as...上看到的一编文章,是个很好的工具,也可以作为如何操作注册表的例子看一下。效果图如下:

 

 

Introduction

介绍

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

我今天早上浏览CodeProject时,在周问题栏中有这样一个问“你的代码段有多长?”。这在论坛中引起一阵讨论,其中Vikram提供了指向Sara Ford写的一篇文章的链接:

http://blogs.msdn.com/saraford/archive/2004/11/15/257953.aspx

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.  So the bottom line is, this 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.

文章讲述了一个小技巧:通过修改注册表,可以在Visual Studio中显示一或多条引导线。而我要讲的是,我想到了一个更好的主意:做一个工具来完成注册表的工作,而你只需使用就可以了。

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

我不会在这重复那篇文章的内容--你可以阅读Sara的Blog(其实就是在注册表的HKEY_CURRENT_USER/Software/Microsoft/VisualStudio/[Vertion(如8.0)]/Text Editor中加入一个键值如("Guides",RGB(0,0,0) 4,80)就可以了--译者注)。我只提供了工具的下载,并讲一下它的原理。

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. Basically, it allows you 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.

这个工具太简单了,根本不值得去重新审视它。我一共只花了10分钟就搞按下了。简单地,它允许你设置引导线的颜色,以及引导线的位置。单击“Apply”提交改变,单击“Remove”删去改变,根本不用动脑子。

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.

C#是一个美妙的语言,但它有时也会让你因怀疑而这抓耳挠腮,在解决方案中徘徊。一个例子是,我想确定那一串引导线位置参数是不是由逗号分隔的数字时,我的第一个想法是(我本身是一个老VB程序员),用Spit()提取分隔的每一顼,然后用IsNumeric()判断是否为数字,可是你瞧,C#中没有这个函数!Char类型的有一个类似的函数,而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:

我的第一个尝试是先Split()字符串,然后从VB中借用IsNumeric():

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.

大部分情况下它能很好地工作,但后来我意识到你仍然可以输入-9、9.5这一类数,或者你可以传一在堆。这些值不会让程序报错,但也没有任何用处,所以我又有了一个新的计划--对于第一部分,我应该用正则表达式来验证输入,它不但减少了我程序的代码,而且工作得很好。

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

源代码下载
posted on 2006-08-16 11:07  Nihgwu  阅读(1087)  评论(0编辑  收藏  举报