Archive for the ‘Tips and Tricks’ Category.

Quick and Easy Google (or Bing) Web Search in Visual Studio

Here is a quick and easy way to add a Google (or Bing) Web Search link to the context menu in Visual Studio:

  • Open the Visual Studio macros IDE by navigating to Tools –> Macros –> Macros IDE (or pressing Alt+F11).
  • Right click on “MyMacros” and select Add New –> Add New Item.
  • Select the Module template, name the module “Search”, and click Add.
  • Paste the following code directly before the “End Module” line:
    Sub Search()
        Dim strUrl As String
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        If selection.Text <> "" Then
            strUrl = "www.google.com/search?q=" + selection.Text
            'strUrl = "www.bing.com/search?q=" + selection.Text
            DTE.ExecuteCommand("View.URL", strUrl)
        Else
            MsgBox("Select Text first to Search")
        End If
    End Sub
    
  • (Optional) If you prefer to use Bing for your search, uncomment the Bing line and comment out the Google line.
  • Your macro editor window should now look something like this:
    image
  • Save and close the Macro Editor window and IDE.
  • Right click on the Visual Studio toolbar and select Customize.
  • Under the Toolbars tab, check the Context Menus option and the switch to the Commands tab.
  • Select the Macros Category, and then select the “MyMacros.Search.Search” macro.
  • Drag the selected macro onto “Editor Context Menus” -> “Code Window” and then drop it where you want it in the context menu (I place mine below the Paste command).
  • Right click on the new context menu item and change it’s name to “&Web Search”.
  • Click close on the Customize window.
  • You are now done! If everything has gone according to plan, when viewing a source code file you should now see a new context menu item that will do a web search on any selected text when you click it:
    image
    image
    • email
    • DotNetKicks
    • Digg
    • del.icio.us
    • Technorati
    • StumbleUpon
    • LinkedIn
    • Facebook
    • Twitter
    • Google Bookmarks
    • Live

Automatically Cancelling a Failed Build in Visual Studio

Stumbled across this little tip on how to automatically cancel a build in progress after getting an error. You normally have to wait for visual studio to try to finish building all of the remaining projects before getting a chance to fix an the issue and this can take a few minutes if you have a lot of projects in your solution.

Step 1: Open the Visual Studio macros IDE by navigating to Tools –> Macros –> Macros IDE.

Step 2: Double click on “MyMacros” and then on “EnvironmentEvents”. You should now be looking at a VB code editor window.

Step 3: Paste the following code directly before the “End Module” line:

Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone

    If Success = False Then
        DTE.ExecuteCommand("Build.Cancel")
    End If

End Sub

Step 4: Save and close the Macro Editor window and IDE.

All done!

  • email
  • DotNetKicks
  • Digg
  • del.icio.us
  • Technorati
  • StumbleUpon
  • LinkedIn
  • Facebook
  • Twitter
  • Google Bookmarks
  • Live

WPF / Silverlight Links

09/15/2009 – Edit: I now have a static page dedicated to these links.

I’m just starting go through my WPF books, so I wanted to capture some useful WPF / Silverlight links that I’ve discovered over the last year.

Windows Client – Winforms and WPF Home. Information, tutorials, links.
http://windowsclient.net/default.aspx

Silverlight Home – Information, tutorials, links.
http://silverlight.net/default.aspx

Web Platform – Microsoft Web Platform Information. Web Platform Installer download.
http://www.microsoft.com/web/default.aspx

Codeplex WPF Home – Portal for accessing the WPF Toolkit and the WPF Futures releases.
http://www.codeplex.com/wpf

WPF Toolkit – Additional Controls, Themes, and Tools for WPF.
http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117

Silverlight Toolkit – Additional Controls, Themes, and Tools for Silverlight.
http://silverlight.codeplex.com/

Patterns and Practicies: Composite WPF and Silverlight (Prism) – WPF /Silverlight Application Framework and Patterns.
http://compositewpf.codeplex.com/

Caliburn – WPF / Silverlight Application Framework.
http://caliburn.codeplex.com/

Visifire – Free (for GPL’d works) Silverlight and WPF Charting Controls. Has dual licensing model for closed source usage.
http://www.visifire.com/

Kaxaml – Lightweight XAML Editor.
http://www.kaxaml.com/

WebAii – Free Web Application Testing Framework for IE and Firefox. V2 Will support Silverlight.
http://www.artoftest.com/products/webaii.aspx

Ninject – Fast, Lightweight .Net Dependency Injection
http://www.ninject.org/

  • email
  • DotNetKicks
  • Digg
  • del.icio.us
  • Technorati
  • StumbleUpon
  • LinkedIn
  • Facebook
  • Twitter
  • Google Bookmarks
  • Live

Retroactive Browser Compatibility

When working on an existing web application you may be placed in a situation where you need to support new browsers, or just other browsers that your app wasn’t originally coded for or tested in. In my case, this took the form of a web app developed exclusively for Internet Explorer that now needed to support other browsers. As I went through and corrected many of the browser incompatibilities, I noticed a few common ones, and wanted to mention them in case it’s beneficial to others in the same situation.

I should mention up front that jQuery was an indispensible tool when I was working on this project as in many cases I was able to replace the offending JavaScript with a jQuery equivalent that was cross browser compatible. If you’re a web developer and are not familiar with jQuery, you probably should be. ;-)

So a couple of things to look out for… I wanted to list the easiest ones to spot first as you can do a text search and usually find all of them:

  1. CSS Expressions:
    • Were implemented in IE5 and allow you to assign a JavaScript expression to a CSS property.
    • Were deprecated in IE8 standards mode, see http://blogs.msdn.com/ie/archive/2008/10/16/ending-expressions.aspx for details.
    • IE specific.
    • Search for: “expression(”
    • Replace with JavaScript that dynamically modifies the CSS properties in response to specific browser events (onresize, etc).
      • In many cases this can be more performant than CSS expressions because you can choose to bind to specific events (CSS expressions are reevaluated *every* time a JavaScript event with at least 1 listener fires.
  2. outerHTML:
    • “Get or set the HTML of the entire node x, including the outermost tag (element x itself).”
    • Example: x.outerHTML = “Let’s <u>change</u> it!”
    • IE specific.
    • Search for: “outerHTML”
    • Replace with innerHTML or W3C DOM methods. innerHTML is in theory faster, but IE and Konquerer have some issues when innerHTML is used with tables. Replacing outerHTML with calls to innerHTML will also require some changes to the logic using outerHTML as innerHTML is slightly different in behavior.
  3. Visual Filters:
    • Used commonly to create gradients without using images.
    • More information on Visual Filters: http://msdn.microsoft.com/en-us/library/ms532853.aspx
    • IE specific.
    • Example: <ELEMENT STYLE=”filter:progid:DXImageTransform.Microsoft.Gradient(sProperties)” … >
    • Search for: “DXImageTransform”
    • Replace with… Unfortunately there really isn’t an equivalent for this. Some of the visual filters can be replicated using standard CSS properties, but not all of them. As convenient as the visual filters are, they will only work in internet explorer at the moment.
  4. Window.Event:
    • Used to access JavaScript event information in IE.
    • IE Specific.
    • Search for: “window.event”
    • This item could have several pages written about it, but the short story is that IE handles passing JavaScript event information differently than other browsers. You will need to code your JavaScript to accommodate this difference. jQuery can again be useful here as it can abstract a lot of this problem away from you. I recommend reading the section on events here: http://www.reloco.com.ar/mozilla/compat.html as it will give you an overview of the problem and how to address it.
  5. Custom Attributes:
    • Used to defined custom element attributes.
    • Getting or setting custom attributes without using DOM methods is not cross browser compatible.
      • Example: element.MyProp = “This doesn’t work in all browsers.”
    • Search for… No good search string for these. You just need to keep your eyes out for JavaScript that makes calls to custom attributes without using DOM methods (or jQuery).
    • Replace with: calls to .getAttribute(“MyProp”) or setAttribute(“MyProp”) or if using jQuery, element.attr() as shown here: http://docs.jquery.com/Attributes/attr.

This list is by no means exhaustive, but I wanted to point out many of the common issues I have come across. Your mileage may vary. I recommend checking out the references listed below for a lot more good compatibility information. I’ve tried to check all of my facts, but if you notice anything that seems off, let me know so I can fix it!

References:

  • http://www.reloco.com.ar/mozilla/compat.html – “Making your web browser compatible with Firefox”
    • A good overview of many of the common browser compatibility problems faced when making an web app written for IE work in Firefox
  • http://www.quirksmode.org/compatibility.html – “Compatibility Master Table”
    • The holy grail of browser compatibility information. Seriously… Want to know if a browser fully supports CSS2, CSS3, DOM, etc? It’s here.
  • http://reference.sitepoint.com/ – “CSS/HTML/JavaScript Reference”
    • Reference site for CSS, HTML, and JavaScript information. Its got a clean layout and additionally contains browser compatibility information for each of the items.
  • http://blogs.msdn.com/ie/archive/2009/03/12/site-compatibility-and-ie8.aspx – “Site Compatibility and IE8”
    • A page detailing potential issues and fixes for making your pages compatible with IE8 in Standards Mode. This is actually a good reference for potential cross browser problems as most of the items listed are cross browser compatibility issues as well.
  • email
  • DotNetKicks
  • Digg
  • del.icio.us
  • Technorati
  • StumbleUpon
  • LinkedIn
  • Facebook
  • Twitter
  • Google Bookmarks
  • Live

Making ReSharper Play Nice With StyleCop

I’ve been toying with StyleCop more and more as of late and ran into some situations where ReSharper and StyleCop were fighting each other over code formatting issues. Some manual tweaking reduced the problems, but it got me curious. So I did some research and uncovered a wonderful tool, StyleCop for ReSharper.

StyleCop for ReSharper is a ReSharper 4.1 plug-in that allows Microsoft StyleCop to be run as you type, generating real-time syntax highlighting of violations. StyleCop for ReSharper also contains a collection of Quick-Fixes and Code Clean-Up Modules to help you easily fix StyleCop code styling violations.

StyleCop for ReSharper also comes with a ReSharper Code Style Settings File which you can import into ReSharper to make ReSharper’s auto formatting features compliant with StyleCop’s recommendations. If you don’t want to import a whole code style settings file and would rather just tweak some specific formatting settings that are responsible for most of StyleCop’s complaints about ReSharper, I recommend reading these 2 blog posts at Hock Blogs:

Navigating around the StyleCop for ReSharper project page on CodePlex also turns up a lot of additional useful information:

All in all, I’m enjoying this ReSharper plug-in. It has taken a lot of the pain out of being compliant with StyleCop’s rules.

  • email
  • DotNetKicks
  • Digg
  • del.icio.us
  • Technorati
  • StumbleUpon
  • LinkedIn
  • Facebook
  • Twitter
  • Google Bookmarks
  • Live