Posts in category .net - page 24

Visual Studio tips & tricks

The Visual Studio IDE (VS2003, VS2005) is a massive beast with a plethora of options, settings and tweaks to be had.

A few of my favorite, often overlooked ones, are:

Adding existing folders

The Add > Existing Item… is all well and good for adding files but how do you add folders to your project?

Specifically folders you’ve already dropped in place, ideally with all the files and sub-folders added.

Simple: Click the Show All Files button at the top of the Solution Explorer_pane then right-click the folder you want included and choose _Include in Project.

Visual Studio 2005 C# Navigation bar window

Speed up large C# projects

VS2003 and 2005 can be sluggish at times especially with large projects.

To get a much-needed speed boost head over to Tools > Options… > Text Editor > C# and uncheck Navigation bar.

Install the C# snippets

On occasion I find simple example snippets of C# code ending up on the front page of DotNetKicks despite Microsoft providing an entire snippet library of C# code to do these things already.

Head over to their download site, install the MSI then choose Tools > Code Snippets Manager… > Visual C# > Add… and enter the folder name that it popped up with post-install.

Open folder in Windows Explorer

From time to time you’ll need to break out of Visual Studio into Windows Explorer and wondered why there is no right-mouse button context menu option available from Solution Explorer.

Just open the file from inside Visual Studio then right-click on the tab and choose Open Containing Folder.

[)amien

QuickReminder mini-app – Time based reminders in your system tray

Screenshot of QuickReminder in actionWhether it’s a meeting for a specific time or remembering to stop the tea brewing in 7 minutes time (lovely tea from Adagio Teas that deserves it’s own blog post) events often whiz by without me noticing. I’m not great at time based background threading… but a computer is.

Sure I fire up Outlook (which I don’t use any more), iCal (Only open when I need it) or Google Calendar, create a new event, choose the type, choose which day and time it occurs at and mentally do the math in my head for 7 minutes from now taking the seconds into consideration…. but that’s distracting enough I think “No, I’ll just remember” and I don’t.

What I want is to make a few clicks to set-up an event – ideally based on previous ones such as another “Green tea brew”. I don’t want audio alarms just a pop-up silent balloon that completely disappears with another single click.

Inspired by App-A-Day I thought “what the hell” and wrote it.

Download QuickReminder (.NET) with full source (29KB).

The source includes a few useful snippets including how to:

  • Access icons/resources in your Resource.resx file without casting or resource managers
  • Make your app register/un-register for automatic startup in Windows
  • Access configuration settings without casting or setting managers
  • Launch a URL from a label
  • Make your system tray based application start-up without a form showing on-screen

Have fun,

[)amien

App-A-Day & SubSonic

Screenshot of WPM Tray in action

App-A-Day

Self-proclaimed Code Jedi Dana Hanna is on a mission to destroy his personal life by writing an application every day for 30 days.

There are a few great apps in there and all come with source – the ones I checked out were in C# :)

Some of my favorites are:

  • WPM Tray – display you word per minute count graph. Also a demonstration of how to hook global key-presses in C#
  • Jedi IRC – a small IRC application as a Visual Studio plug-in – both interesting things to see done in C#
  • Mouse Heat Map – get a display of where your mouse lives on your desktop! :D

I must confess these tiny fun apps have inspired a couple of my own that I’ll hopefully knock up and post at some point.

SubSonic – The Zero Code DAL

In other news SubSonic has come on a lot having addressed all the major issues apart from still being ActiveRecord based over DataMapper pattern. (If you haven’t seen it before check out the 20 minute screen-cast)

Rob, the brains behind SubSonic, has been interviewed over at the DotNetRocks podcast and I recommend giving this one a go – don’t let the first 5-10 minutes of warm-up and ads put you off.

He’s also put together a starter site solution you can pick-up from the same place which has some useful bits.

[)amien

Extending GridView to access generated columns

ASP.NET’s GridView is a useful control and one of it’s best features is it’s ability to generate the columns automatically from the data source given.

The problem however is that these generated columns are not exposed as part of the Columns collection or indeed available at all so you can’t hide or manipulate the selected columns.

One simple scenario might be that you want the first column to be a “View” link to drill down into the row displayed. Whilst you can add the column to the GridView before data binding you can’t actually pull out the information needed from another columns to construct the URL.

By sub-classing GridView you can obtain this functionality with some caveats.

Version 1: Auto generated columns added to the Columns collection… with caveats.

using System;
using System.Data;
using System.Collections;
using System.Web.UI.WebControls;

public class GridViewEx1 : GridView
{
    private DataControlFieldCollection originalColumns;

    public GridViewEx1() : base() {
    }

    public void RecordColumns() {
        originalColumns = new DataControlFieldCollection();
        foreach(DataControlField column in Columns)
            originalColumns.Add(column as DataControlField);
    }

    public void ResetColumns() {
        if (originalColumns == null)
            RecordColumns();
        else {
            Columns.Clear();
            foreach(DataControlField column in originalColumns)
                Columns.Add(column as DataControlField);
        }
    }

    protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource) {
        ResetColumns();
        ICollection generatedColumns = base.CreateColumns(dataSource, useDataSource);
        foreach(DataControlField column in generatedColumns)
            if (!originalColumns.Contains(column))
                Columns.Add(column as DataControlField);
        return Columns;
    }
}

This version provides some compatibility with existing code/expectations in that the auto-generated columns are part of the Columns collection after the DataBind.

Should you call DataBind again however as well as wiping out the changes to the generated columns (they are, after all re-generated) any additional columns added to the collection after the first DataBind will also be lost as it does not track which are added by the programmer and which automatically.

Version 2: All bound columns exposed as BoundColumns, user ones as Columns.

using System;
using System.Data;
using System.Collections;
using System.Web.UI.WebControls;

public class GridViewEx2 : GridView
{
    private DataControlFieldCollection boundColumns = new DataControlFieldCollection();

    public GridViewEx2() : base() {
    }

    public DataControlFieldCollection BoundColumns {
        get { return boundColumns; }
    }

    protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource) {
        ICollection generatedColumns = base.CreateColumns(dataSource, useDataSource);
        BoundColumns.Clear();
        foreach (DataControlField column in generatedColumns)
            BoundColumns.Add(column as DataControlField);
        return BoundColumns;
    }
}

After the DataBind you will have full access to the generated columns as part of the BoundColumns collection.

[)amien