jump to navigation

Copy and Paste Formatting with Visual Studio’s Dark Theme May 17, 2013

Posted by codinglifestyle in CodeProject, Visual Studio, Visual Studio 2012.
Tags: , , , , , ,
12 comments

I recently upgraded to VS2012 and, like most of you, was aghast at the default theme. Sure, after installing the updates the blue theme was good. But before updating I tried the dark theme… and I liked it! As our phones and tablets often use dark themes and websites are being remade to look like tablet apps the dark theme had a modern look to it and is easy on the eyes.

Look, it doesn’t matter what I think of the dark theme. I simply want to discuss an undesirable side effect affecting Copy & Paste (don’t forget Cut too). When copying code to an email, Word, or your IM window you realize the dark theme has a dark side:

What we get in the first paste attempt is a WYSIWYG copy of the formatting from Visual Studio. Clearly what most of us want is the second paste attempt. And the formatting issue can get even worse. When pasting to some programs you get white text on a white background. The problem is so obvious it seems the people who made the dark theme don’t actually use it day to day.

I was finding that I needed to: open options, select the blue theme, copy my code to the clipboard, open options again, and reselect the dark theme.

What a tedious workaround! So I went on a quest this morning and am happy to report I have a solution!

  1. Open Tools → Extensions and Updates
  2. Select Online (Visual Studio Gallery) and search for Productivity Power Tools 2012
  3. Download and restart Visual Studio when prompted
  4. Open Tools → Options
  5. Expand Productivity Power Tools and select HTML Copy

  1. Change the BeforeCodeSnippet option to:
    1. <style type=”text/css”>.identifier {color:black !important;}</style><pre style=”{font-family}{font-size}{font-weight}{font-style}”>
  2. Change EmitSpanClass to:
    1. True
  3. Check EmitSpanStyle is:
    1. True

You may want to optionally turn off all other features other than HTML Copy from the “All Extensions” menu.

Let’s take a look at what this feature is doing. When you copy text to the clipboard you can have multiple data formats such as Text, RTF, and HTML. When we’re pasting our code in to Word or an email it will typically use the HTML format (configuration dependent). Here is what we actually see in the clipboard when we do a copy after configuring VS as above.

</pre>
<!--StartFragment-->
<style type="text/css">
 .identifier {
 color: black !important;
 }
</style>
<pre style="font-family: Consolas; font-size: 13;"><span class="keyword" style="color:#569cd6;">var</span>&nbsp;<span class="identifier" style="color:white;">bob</span>&nbsp;<span class="operator" style="color:#b4b4b4;">=</span>&nbsp;<span class="keyword" style="color:#569cd6;">string</span><span class="operator" style="color:#b4b4b4;">.</span><span class="identifier" style="color:white;">Empty</span>;
</pre>
<!--EndFragment-->

Note that we’ve removed the background colour which means all of our identifier text is being set as white on a white background. However the style element is overriding the identifier color and setting it to black. This gives us the desired results:

var bob = string.Empty;

Advertisement

VS Solution Explorer – Collapse All May 15, 2009

Posted by codinglifestyle in CodeProject, Visual Studio.
Tags: , , , , ,
1 comment so far

I never knew I needed this until I saw it was a feature in a ReSharper demo.  After a quick search and I found a macro that has been around for years.  Slap to the head!  The amount of time I spend collapsing all my solution folders and this existed the whole time!

I never use macros, but even I was able to quickly get this working.

  1. Open Tools->Macros->Macro Explorer (Alt-F8)
  2. Right click MyMacros
  3. Select New Module
  4. Select default template and name to VSMacros
  5. Right-click VSMacros and click New Macro
  6. Copy and paste this over the opened macro file

    Imports System

    Imports EnvDTE

    Imports EnvDTE80

    Imports EnvDTE90

    Imports System.Diagnostics

    Public Module VSMacros

    Sub SolutionCollapseAll()

    ‘ Get the the Solution Explorer tree

    Dim solutionExplorer As UIHierarchy

    solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

    ‘ Check if there is any open solution

    If (solutionExplorer.UIHierarchyItems.Count = 0) Then

    Return

    End If

    ‘ Get the top node (the name of the solution)

    Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)

    rootNode.DTE.SuppressUI = True

    ‘Find selected node

    Dim selectedNode As UIHierarchyItem = FindSelectedNode(rootNode, solutionExplorer)

    ‘ Collapse each node

    Collapse(selectedNode, solutionExplorer)

    ‘ Select the selceted node

    selectedNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

    rootNode.DTE.SuppressUI = False

    End Sub

    Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)

    For Each innerItem As UIHierarchyItem In item.UIHierarchyItems

    If innerItem.UIHierarchyItems.Count > 0 Then

    If innerItem.UIHierarchyItems.Expanded Then

    ‘ Re-cursive call

    Collapse(innerItem, solutionExplorer)

    End If

    ‘ Collapse

    If innerItem.UIHierarchyItems.Expanded Then

    innerItem.UIHierarchyItems.Expanded = False

    If innerItem.UIHierarchyItems.Expanded = True Then

    ‘ Bug in VS 2005

    innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)

    solutionExplorer.DoDefaultAction()

    End If

    End If

    End If

    Next

    End Sub

    Private Function FindSelectedNode(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy) As UIHierarchyItem

    If item.IsSelected Then

    Return item

    End If

    For Each innerItem As UIHierarchyItem In item.UIHierarchyItems

    If innerItem.UIHierarchyItems.Count > 0 Then

    If innerItem.UIHierarchyItems.Expanded Then

    ‘ Re-cursive call

    Dim result As UIHierarchyItem

    result = FindSelectedNode(innerItem, solutionExplorer)

    If Not result Is Nothing Then

    Return result

    End If

    End If

    If innerItem.IsSelected Then

    Return innerItem

    End If

    End If

    Next

    Return Nothing

    End Function

    End Module

     

  7. You may now execute the macro via the Macro Explorer.  Click the folder you wish to collapse (or the Solution root to collapse everything).  Right click and run the macro.  To set a shortcut continue on with the steps below…
  8. Click Tools->Options->Environment->Keyboard
  9. Search for your macro in the Show commands containing textbox
  10. Select your macro, keep scope global, and in the assign macro keys textbox enter your preferred shortcut (I used “ALT-CTRL-\”)
  11. Click ASSIGN and then OK
  12. To use, select a node and then ALT-CTRL-\.  To collapse the entire solution, select the root of the solution.

Reference: http://www.codeproject.com/KB/macros/collapseall.aspx?df=100&forumid=7565&exp=0&select=396167

Code Snippets – List of C# snippets in VS2008 May 6, 2009

Posted by codinglifestyle in C#, CodeProject, Visual Studio, Visual Studio 2010.
Tags: , , ,
add a comment

In VS2010 we are told to expect a lot more code snippets.  To be honest, I don’t use them much but watching the demo I told myself it was time to give them another look.  Sometimes I get a great feeling of productivity in hammering out for loop templates and properties and tell myself who needs snippets?  But there is a lot of power there, and its going even further in VS2010.

My problems is I can never remember which code snippets are there.  I occassionaly see them in Intellisense, but there is so much there it’s hard to pick them out.  More than once I’ve carefully gone through the intellisense list trying to pound the useful ones in to my brain.

I decided to do a quick search for a list of code snippets in VS2008 for C#.  Surely I can’t be the first who just wants a simple list, right?  I came across Francesco’s blog who posted the code to iterate through your snippets and output them.  A quick change to update it for VS2008, C#, and to output a comma delimited line per snippet, then a paste in to Word, and Convert Text to Table (by comma) and voila!

List of Code Snippets in VS2008 for C#

#if Code snippet for #if
#region Code snippet for #region
attribute Code snippet for attribute using recommended pattern
checked Code snippet for checked block
class Code snippet for class
ctor Code snippet for constructor
~ Code snippet for destructor
cw Code snippet for Console.WriteLine
do Code snippet for do…while loop
else Code snippet for else statement
enum Code snippet for enum
equals Code snippet for implementing Equals() according to guidelines
exception Code snippet for exception
for Code snippet for ‘for’ loop
foreach Code snippet for foreach statement
forr Code snippet for reverse ‘for’ loop
if Code snippet for if statement
indexer Code snippet for indexer
interface Code snippet for interface
invoke Code snippet for safely invoking an event
iterator Code snippet for a simple iterator
iterindex Code snippet for ‘named’ iterator/indexer pair using a nested class
lock Code snippet for lock statement
mbox Code snippet for MessageBox.Show
namespace Code snippet for namespace
prop Code snippet for an automatically implemented property
propg Code snippet for an automatically implemented property with a ‘get’ access or and a private ‘set’ accessor
sim Code snippet for int Main()
struct Code snippet for struct
svm Code snippet for ‘void Main’ method
switch Code snippet for switch statement
try Code snippet for try catch
tryf Code snippet for try finally
unchecked Code snippet for unchecked block
unsafe Code snippet for unsafe statement
using Code snippet for using statement
while Code snippet for while loop

You can see snippet’s keyword typically matches the operator.  So next time you type for or while or try remember to hit TAB-TAB to expand the snippet.  Next time we’ll look at making your own code snippets.

VS2005 Web Setup Projects August 17, 2006

Posted by codinglifestyle in ASP.NET, Visual Studio.
Tags: , , ,
add a comment

Upon wrapping up my first major project on VS2005 I went to create a web setup project and noticed what I thought was a bug.  When adding Project Output and selecting my website, only Content files were listed.  This would include source code rather than being a release version of the project.  Being that this behavior differs from VS2003 my collegues were at a loss to explain it and googling didn’t yeild much.  I was forced to Add Files which referenced the files I wanted but was sure this wasn’t correct.  After another round of googling I found the answer:

For some reason beyond me, you need to create an interim project which will hold the deployment of the web site.  You may then add this project to your setup and select Project Output.  But wait, there’s more.  You can’t create a deployment project out of the box, because they didn’t seem fit to include it as standard.

  1. So, what you need to do first is download the Web Deployment setup from Microsoft which allows you to create the interim deployment project: http://msdn.microsoft.com/asp.net/reference/infrastructure/wdp/default.aspx 
  2. After this is installed and you restart VS2005, right click your web project and select “Add deployment project”.  This project allows you to specify how you’d like your assemblies compiled and few other options.
  3. Now create a Web Setup Project, add Project Output, select the deployment project and the configuration.

Congratulations, you’ve now done in three steps what you used to be able to do in two.