Archive for Programming

16 Dec 2013

Avatars with Active Directory

No Comments ASP.NET and Programming

When given the choice of either creating another login/password and another place to store details, or using an existing one (Active Directory), the second is usually a better choice.

With AD able to store images uploaded by a user, it’s possible to retrieve them just like any other property. This generic handler will retrieve, resize, then cache an image from Active Directory. If the user doesn’t have an image a generic one will be inserted into the cache and served instead.

This saves the load of hitting the AD server for large images every time you need a user thumbnail.

Two query string parameter are required, **size** and **username** which is the sAMAccountName.
Read more

14 Aug 2013

Sliding Deletion of Rows with ASP.NET

No Comments ASP.NET

One of the nice touches in GMail is the sliding ‘undo’ message when you delete an email. I wanted to replicate this in an ASP.NET ListView when an item was deleted.

The delete action is triggered by a Button in the ItemCommand handler for the ListView. The ListView in my case is wrapped in an UpdatePanel for smoother transition.
Read more

07 Aug 2013

Recognition of free-text durations

No Comments ASP.NET and WPF

There are often times when creating a UI to allow a user to choose a preset list doesn’t work, due to lack of space or too many available options.

A shining example of this is RememberTheMilk’s Add Task box, which recognizes pretty much whatever you throw at it.

This is my take on recognizing free text as a duration and converting it to a TimeSpan in .NET.
Read more

31 May 2013

Binding the SelectedColor of a WPF ColorPicker

No Comments WPF

The ColorPicker is a free control in the XCeed WPF Toolkit for choosing a color from a list. I recently needed to bind it’s value to a DependencyProperty stored as a hex string on an object. After trying and failing with creating a second property which exposed the hex color code a a System.Windows.Media.Color I settled on using a converter, you can bind it like this:

<xctk:colorpicker SelectedColor="{Binding PrimaryColor,Converter={StaticResource ColorHexConverter}}" ShowStandardColors="true" ShowAvailableColors="true"></xctk:colorpicker>

Read more

06 May 2013

Nested Navigation With Umbraco

No Comments ASP.NET

Navigation can always be fiddly with Umbraco with XSLT. This template take two parameters, startNode and maxLevel. It recursively prints a hyperlinked list of nodes so you end up with a nested list of <ul>‘s going maxLevel levels deep from startNode.

Read more

05 Apr 2013

Generic Comparing and Merging of Lists

No Comments Programming

While writing the database synchronization code for a number of apps, I’ve found myself writing method after method of boilerplate which does more or less the same thing. This generic method uses a sorted collection comparer, which I can’t claim credit for. I believe it was originally from StackOverflow.

The SortedCollectionComparer compares two lists using an IComparer, performing one action when an item only exists in one list (eg: adding it to a combined list), and another action when an object is in both lists (eg: comparing last modified time).

This lets you write (relatively) little boilerplate to merge two lists:
This is done using an intermediate method to call SortedCollectionComparer.CompareSortedCollections.

public List<locationbase> SynchronizeLocations(string otherDbPath) {
    SqLiteDal otherDb = new SqLiteDal(otherDbPath);
    var merged = new List</locationbase><locationbase>();
    this.SynchronizeItems</locationbase><locationbase>(() => this.GetLocations(), () => otherDb.GetLocations(), merged.Add, (loc1, loc2) =>
        {
            if (loc1.LastModified > loc2.LastModified) {
                merged.Add(loc1); // first item newer
            } else {
                // either they're equal or the one in the second database is newer, either way we want the one from the second Db.
                merged.Add(loc2);
            }
    }, new LocationComparer());
    return merged;
}
</locationbase>

Read more

17 Mar 2013

Vanilla Forums to YAF.NET Conversion

No Comments Programming and WPF

Migrating data between applications is always a fiddly process. Ideally someone has already written a converter which allows you to import from a generic format (eg: XML) but in the case of my transition from Vanilla forums to YAF.NET, nothing existed already and I had to start from scratch.

First I tried using SSIS to take a flat file (exported from Vanilla) into SQL Server. This took the best part of a day due to all of the conversions involved and the difficult to parse format used by Vanilla. After a day struggling I eventually decided to give up and do it in .NET.

This WPF utility needs a little modification before it will work for you, it should at least give you a huge headstart in the process. I used SSIS to import the users so I’ll skip that step.

  1. Export the gdn_users (Users), gdn_discussion (Topics), gdn_category (Forums) and gdn_comment (Posts) tables using phpMyAdmin to XML.
  2. Open the Constants.cs in the solution and update the paths to point to these files.
  3. Compile and run the app and hit the ‘Go’ button
  4. Disco

This will import your forums, topics and posts from Vanilla. The only thing it won’t do is correctly arrange subforums, so you’ll still need to re-arrange the forums afterwards to re-nest forums. The main thing for me was getting the post data across, but the app could easily be extended to import any other important data from Vanilla.

10 Mar 2013

jQuery Toolbar Placeholder Plugin

No Comments JavaScript

In creating a templating system for people to insert placeholder text, I wanted a toolbar with buttons which would insert text at the position of the cursor when clicked.
jQuery toolbar plugin
This plugin takes an array of button IDs and the text to be inserted when each button is clicked. EG:L

var buttons = [
	{ 'buttonId':'btnName', 'insertionText':'%%NAME%%' },
	{ 'buttonId':'btnAddress', 'insertionText':'%%ADDRESS%%' },
];
$('#toolbar').hypotoolbar(buttons, 'txtBody');

When the btnName and btnAddress buttons are clicked, %%NAME%% and %%ADDRESS%% will be inserted into the textarea with ID txtBody at the cursor position.
Read more

22 Feb 2013

jQuery Text Progress Meter

No Comments JavaScript

This small plugin will display a progress meter which fills up as text is entered into an input or textarea. Useful for indicating to a user how much remaining text they have in a field with a maximum length.

jQuery.textprogress Demo

Options

  • messageFormat – The format for the count label. Available placeholders are {current}, {total} and {percent}
  • showCounttrue/false – Whether or not to display a total of the current length/maximum allowed length.
  • max – The maximum allowed length
  • allowOverLengthtrue/false – Whether or not to prevent more text being entered than allowed.
  • classes An object containing two keys:
    • standard – The progress bar class if less than the allowed length of text is entered.
    • over – The class if equal to or more than the allowed text length is entered
  • textbox – The textbox/input element to measure.

Demo
Source on GitHub

17 Feb 2013

Bootstrap CSS Image Buttons

No Comments JavaScript and Programming

One of the repetitive things I found myself doing with Twitter’s Bootstrap framework was creating buttons which had a background gradient and an image. (Doesn’t work in < IE9, but does in Fx, Chrome and Safari).This makes use of the multiple backgrounds CSS3 property. The CSS I was typing every time was this (eg: for an ‘add’ button):
Read more