Live Fast, Code Hard, Die Young

Posts tagged ‘visual studio’

Improving current line highlighting in VS2010

Are you experiencing that VS2010 is suddenly highlighting the current line in the text editor? Do you want to get rid of it? I’ll show you how – or maybe convince you to keep it!

 

Line highlighting

First of all, let me explain what I’m talking about in case you haven’t figured yet. Current line highlighting is when the entire line that your cursor is on is shown in a different color. In my case it looks like this:

image

This is not part of the default functionality in VS2010 so most users never see this. However, if you happen to install the Productivity Power Tools it will be enabled by default.

The idea is that highlighting the current line will make it easy to see where your cursor is when you have a large monitor and get lost.

Some users probably love this feature. Personally I don’t like it at all when my cursor “taints” the current row like this when I’m editing. I find it plain annoying!

 

Turning it off

The first solution if you don’t like it is obviously to turn it off completely. This is pretty easy to do using the settings which you can find under Tools->Options and then Productivity Power Tools. Just flick the switch to Off:

image

Now if that was all I had to say this wouldn’t be a useful blog post. Let’s see what we can do!

 

Making it work better

Instead of turning it off we can make better use of this feature. I actually think it is a very good idea to show where the cursor is. However, once I have found the place and start typing the highlighting is just in the way for me.

I have come up with a better solution that involves changing the color of the line depending on if the edit window is active or not. This is possible through the settings under Environment->Fonts and Colors. There are two entries for the Current Line color, one for when the line is active and one when it is inactive:

image

I have set the active color to White which means that when I’m in the edit window and typing the highlighting is hidden. When I navigate classes in the Solution Explorer or elsewhere it will light up. This will effectively work very nicely because when I’m working with other panels or windows and returning to the edit window I can find where I was pretty quickly.

Give it a try and see if it works for you! 🙂

 

Conclusion

Line highlighting is a pretty controversial feature. Some swear by it and some curse it. I hope my post shows one interesting hybrid way of using it that does not get in the way as much as the default behavior.

The Productivity Power Tools is a really nice addition to Visual Studio and I highly recommend it. Through the settings you can customize it even more if you don’t like how it works. I suggest you play around with it and see if you can find other useful tips and tricks to share. I’d love to hear about them so feel free to leave a comment on my blog! 🙂

Altering database tables with VS2010

One thing I find quite annoying in Visual Studio is when I try to modify an existing database table and get the following error dialog that prevents me from saving:

image

“You cannot save changes that would result in one or more tables being re-created”

So, what the heck is up with that? This seems to appear when you move columns around or change the “Allow Nulls” setting. I know I could do this in earlier version of Visual Studio so why can’t I do it anymore? Yes, I know I’m changing the table structure fundamentally but I know what I’m doing and it should be safe to do it – just let me save please!

Well, the wall of text in the dialog actually tells you what to do. You have to enable this in the VS options. For some reason this is disabled by default and my guess is that it is to prevent people who have no clue what they are doing from destroying the database schema…

Anyway here is where you find the option that you should disable. It is called “Prevent saving changes that require table re-creation”:

image

Does this mean that you entire table data will be erased when you make a change? No, certainly not! All it means is that your data will be copied to a temporary table and then copied back when the table has been altered. I guess this is how it has always worked but before you never had to bother about it – it just worked.

Well, thanks to this little option it now works again!

Adjust namespaces with Resharper 5

A really nice feature in Resharper 5 is the ability to adjust namespaces for code files in a directory. When you move files around in your project or between different projects the namespace no longer matches the directory structure. Updating the namespaces in the code files by hand is quite tedious and error prone. This is especially true when you move user controls or web pages that consists of a XAML file or ASP.NET page with a code behind class that needs to match.

You can adjust the namespace for all files in a directory by simply right clicking the folder in Visual Studio and select “Refactor->Adjust Namespaces…”:

image

This will give you the following dialog where you can see the changes that Resharper suggests:

image

Quite handy I think!

Visual Studio debugging tricks

I learned a new Visual Studio trick today. I’m currently working on a hobby project in C++ and since the plan is to make it cross platform I’m using the excellent Juce framework.

Juce has its own string type which is very nice. It handles Unicode, reference counting and so on. Its like the CString type but cross platform. Some people may prefer the standard lib string std::string but personally I think the std lib is a bit awkward.

Anyway, so what’s my problem then? Well, the thing is that when you debug your program in Visual Studio you really want to see the contents of strings easily. By default you get this when using the juce::String type:

image

Visual Studio has no knowledge about the juce::String type so it just takes a guess at what we want to see and shows us some of the private members in the type. Not very helpful…

Autoexp.dat
So what can we do about it? Well I found some info about a little file containing debugger settings for Visual Studio. It’s default location is under C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger and the file is called autoexp.dat. This file controls how VS displays different types during debugging. Interesting!

If you are running Vista/Win7, make sure you edit autoexp.dat as administrator because it’s protected since it’s in your program files. Otherwise you end up writing to a file that gets placed in your VirtualStore and it won’t work (yes, I did this myself..doh!)

Note that you can change the autoexp.dat file anytime. It will be reloaded when the debugger starts, so you don’t have to restart VS.

Now lets see what we can do… Open up the file autoexp.dat and scroll down a bit to a section called [Visualizer]. This section controls the VS visualizers for STL, ATL and other stuff. This part has a large DO NOT MODIFY text so we won’t touch that part (make a backup if you’re scared…)

Just below the part with the STL and ATL container stuff add the following:

juce::String{
    preview     ([$e.text->text,su])
    stringview  ([$e.text->text,sub])
}

This will tell VS about the Juce string type and explain how we want to view it. The ‘preview’ bit controls what to show in the watch window and the ‘stringview’ bit controls the popup string viewer that allows you to view the string as text, XML or HTML. Very handy feature!

The expression refers to the variable being viewed as $e and then you simply access its members as you would in normal code watch expressions. The ‘su’ part means that the value should be treated as a null terminated string in Unicode format. The ‘sub’ means that the string should be shown using “bare representation”. In this case it will display the string without the surrounding quotes (I only use this in the string viewer, as this seems to be the common practice).

Here is a screenshot of how it looks now:

image

Neato! Now we see the strings easily while debugging. We can also click the magnifying glass to examine the string in a popup window, even show it as formatted XML. Now the Juce type is even more juicy! Great isn’t it?

These debugger expressions can do a lot more. You can read more it here: http://www.virtualdub.org/blog/pivot/entry.php?id=120

Happy coding!