views:

55

answers:

3

In VB Classic, VBA and also in Visual Studio you can dbl click close to the left hand margin of a block of code and it will select the whole of the current block (sub, function etc). In Visual Studio this clickable area is adjacent to the line numbers on the left.

Is there a keyboard shortcut that will do the same job? that is, select the "current block".

A: 

For C#, you can get a similar effect by placing your cusrsor beside an opening brace and hitting Shift-Ctrl-} to select all code between the braces. This and other good stuff here.

Or, for VB, a Visual Studio macro to select from wherever you place the cursor to the next occurrence of End Sub. Apply this to your own keyboard shortcut and you should have something pretty workable:

Sub SelectToEndSub()
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    Dim lStartLine As Long = objSel.TopPoint.Line
    objSel.FindPattern("End Sub")
    Dim lEndline As Long = objSel.TopPoint.Line
    objSel.GotoLine(lStartLine)
    objSel.LineDown(True, lEndline - lStartLine + 1)
End Sub
Chris Wallis
is there something similar for vb.net?
hawbsl
Apologies; I should have realised you were talking VB. I can't find a keyboard shortcut but it would be possible with a macro. I might get back to you...
Chris Wallis
no apology necessary, tried your thing anyway and also tried possible vb equivalents but no dice. the link was interesting too.
hawbsl
Have a go with the Visual Studio macro above. Pretty noddy but seems to work in my brief test
Chris Wallis
A: 

I was looking for the same thing when I came across CodeRush Xpress (free) from DevExpress. You can get more information here: http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/

After installing CodeRush Xpress, you can select blocks of code by hitting a keyboard shortcut. What's great about this implementation is that it lets you increase or decrease the scope of your selection with each keypress.

For example:

Private Sub MySub ()
  If myBooleanValue Then
    txtResult.text = "The quick brown fox jumps over the lazy dog"
  Else
    txtResult.text = "nevermind"
  End If
End Sub

Place your cursor before the "z" in "lazy", then increase the scope of your selection with the keyboard shortcut (I bound mine to the "+" on the numberpad,) by repeatedly increasing the scope of your selection, you get the following selections:

  • Press 1: The quick brown fox jumps over the lazy dog
  • Press 2: "The quick brown fox jumps over the lazy dog"
  • Press 3: txtResult.text = "The quick brown fox jumps over the lazy dog"
  • Press 4: (entire If Then statement selected)
  • Press 5: (entire Sub selected)

Pressing the keyboard shortcut for decreasing the selection will have the reverse affect.

I HIGHLY recommend checking CodeRush Xpress out. I liked it so much I bought the full version (which has been worth every penny.)

knslyr
A: 

Using ReSharper, repeatly press Ctrl-w it'll quickly select the desired scope.

Carlo V. Dango