James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 136 , comments - 1076 , trackbacks - 0

My Links


Welcome to my blog! I'm a Sr. Software Development Engineer in Seattle, WA. I've been doing C++/C#/Java development for over 18 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

Blogs I Read

MCC Logo MVP Logo

Follow BlkRabbitCoder on Twitter

Tag Cloud


Post Categories

Visual Studio Little Wonders: Box Selection

So this week I decided I’d do a Little Wonder of a different kind and focus on an underused IDE improvement: Visual Studio’s Box Selection capability. This is a handy feature that many people still don’t realize was made available in Visual Studio 2010 (and beyond).  True, there have been other editors in the past with this capability, but now that it’s fully part of Visual Studio we can enjoy it’s goodness from within our own IDE.

So, for those of you who don’t know what box selection is and what it allows you to do, read on!

Sometimes, we want to select beyond the horizontal…

The problem with traditional text selection in many editors is that it is horizontally oriented.  Sure, you can select multiple rows, but if you do you will pull in the entire row (at least for the middle rows).  Under the old selection scheme, if you wanted to select a portion of text from each row (a “box” of text) you were out of luck.  Box selection rectifies this by allowing you to select a box of text that bounded by a selection rectangle that you can grow horizontally or vertically. 

So let’s think a situation that could occur where this comes in handy. Let’s say, for instance, that we are defining an enum in our code that we want to be able to translate into some string values (possibly to be stored in a database, output to screen, etc.).

Perhaps such an enum would look like this:

   1: public enum OrderType
   2: {
   3:     Buy,        // buy shares of a commodity
   4:     Sell,       // sell shares of a commodity
   5:     Exchange,   // exchange one commodity for another
   6:     Cancel,     // cancel an order for a commodity
   7: }

Now, let’s say we are in the process of creating a Dictionary<K,V> to translate our OrderType:

   1: var translator = new Dictionary<OrderType, string>
   2:     {
   3:         // do I really want to retype all this???
   4:     };

Yes the example above is contrived so that we will pull some garbage if we do a multi-line select. I could select the lines above using the traditional multi-line selection:


And then paste them into the translator code, which would result in this:

   1: var translator = new Dictionary<OrderType, string>
   2:     {
   3:         Buy,           // buy shares of a commodity                    
   4:         Sell,          // sell shares of a commodity                    
   5:         Exchange,      // exchange one commodity for another                    
   6:         Cancel,        // cancel an order for a commodity                                
   7:     };

But I have a lot of junk there, sure I can manually clear it out, or use some search and replace magic, but if this were hundreds of lines instead of just a few that would quickly become cumbersome.

The Box Selection

Now that we have the ability to create box selections, we can select the box of text to delete!  Most of us are familiar with the fact we can drag the mouse (or hold [Shift] and use the arrow keys) to create a selection that can span multiple rows:


Box selection, however, actually allows us to select a box instead of the typical horizontal lines:


Then we can press the [delete] key and the pesky comments are all gone! You can do this either by holding down [Alt] while you select with your mouse, or by holding down [Alt+Shift] and using the arrow keys on the keyboard to grow the box horizontally or vertically.

So now we have:

   1: var translator = new Dictionary<OrderType, string>
   2:     {
   3:         Buy,      
   4:         Sell,     
   5:         Exchange, 
   6:         Cancel,   
   7:     };
Which is closer, but we still need an opening curly, the string to translate to, and the closing curly and comma. Fortunately, again, this is easy with box selections due to the fact box selection can even work for a zero-width selection! That is, hold down [Alt] and either drag down with no width, or hold down [Alt+Shift] and arrow down and you will define a selection range with no width, essentially, a vertical line selection:


Notice the faint selection line on the right? So why is this useful? Well, just like with any selected range, we can type and it will replace the selection. What does this mean for box selections? It means that we can insert the same text all the way down on each line! If we have the same selection above, and type a curly and a space, we’d get:


Imagine doing this over hundreds of lines and think of what a time saver it could be! Now make a zero-width selection on the other side:


And type a curly and a comma, and we’d get:


So close! Now finally, imagine we’ve already defined these strings somewhere and want to paste them in:

   1: const private string BuyText        = "Buy Shares";        
   2: const private string SellText       = "Sell Shares";        
   3: const private string ExchangeText   = "Exchange";        
   4: const private string CancelText     = "Cancel";

We can, again, use our box selection to pull out the constant names:


And clicking copy (or [CTRL+C]) and then selecting a range to paste into:


And finally clicking paste (or [CTRL+V]) to get the final result:

   1: var translator = new Dictionary<OrderType, string>                
   2: {                    
   3:     { Buy,         BuyText      },                    
   4:     { Sell,        SellText     },                    
   5:     { Exchange,    ExchangeText },                    
   6:     { Cancel,      CancelText   },                         
   7: };

Sure, this was a contrived example, but I’m sure you’ll agree that it adds myriad possibilities of new ways to copy and paste vertical selections, as well as inserting text across a vertical slice.


While box selection has been around in other editors, we finally get to experience it in VS2010 and beyond. It is extremely handy for selecting columns of information for cutting, copying, and pasting. In addition, it allows you to create a zero-width vertical insertion point that can be used to enter the same text across multiple rows.

Imagine the time you can save adding repetitive code across multiple lines!  Try it, the more you use it, the more you’ll love it!

Print | posted on Tuesday, October 30, 2012 7:26 PM | Filed Under [ My Blog C# Software .NET Little Wonders ]



# re: Visual Studio Little Wonders: Box Selection

Sure, box selection can be handy (and I use it often myself) but in this particular example I would rather use regular expression search-and-replace. It would be much faster, in my opinion.
10/31/2012 2:34 AM | Pavel

# re: Visual Studio Little Wonders: Box Selection

@Pavel: true, regular expressions are powerful for search and replace, but often times they can take some finesse to get them just right. I find the box selection and especially zero-width box-selection come in extremely handy and tend to be easier to use especially for those less familiar with regular expression matching.

That said, of course, artists choose the tools they favor most. I'm just glad VS has added a new tool for people to choose from :-)
10/31/2012 6:58 AM | James Michael Hare

# re: Visual Studio Little Wonders: Box Selection

I find it very anoying that Microsoft SQL Server Management Studio has an extremely limited version of box selection, it's practically nihil.
10/31/2012 8:22 AM | Peter M

# re: Visual Studio Little Wonders: Box Selection

SSMS2012 is built on the VS2010 Shell, so should have the full box selection capabilities (not yet tried them - didn't know about them till today!).
10/31/2012 9:47 AM | Dave

# re: Visual Studio Little Wonders: Box Selection

Note that VS2005 has box selection but it's fairly limited. You can delete, cut, copy and paste but not type into it correctly. It also won't allow 0 width
11/2/2012 6:43 AM | Deanna

# re: Visual Studio Little Wonders: Box Selection

That feature doesn't look like much but oh how I miss it when I'm using other editors! Thanks for spreading the word, it's surprising how few people know this.
11/4/2012 7:25 PM | Tristan
Post A Comment

Powered by: