Programming Reference Cheat Sheets

I recently needed to understand PHP for a project I was helping with.  As this was not my main language I needed to find a PHP quickstart of the language to get me going.  These programming reference sheets from Dreamincode.net helped a lot.  Currently you can download the following reference sheets in PDF format.

See here for the full list.


Integrating Silverlight into Webform Master Pages

I have been getting to grips with silverlight over the past couple of weeks.  I followed this webcam tutorial on silverlight.net.  The sample page displays well in IE, Firefox and Chrome, this was good but I wanted integrate silverlight into the rest of my site which uses master pages.
To accomplist this I did the following:

  • Moved Silverlight.js from the root to the site scripts folder.
  • Moved the on page javascript to a js file in the site scripts folder, named SilverlightClient.js
  • Moved the on page styles to the site styles folder, named silverlight.css
  • Create a new content page and added the relevant javascript and stylesheets previously mentioned.  Also added the silverlight section from the body of the sample page.

   1:      <link href="Styles/Silverlight.css" rel="stylesheet" type="text/css" />
   2:      <script type="text/javascript" src="Scripts/Silverlight.js"></script>
   3:      <script type="text/javascript" src="Scripts/SilverlightClient.js"></script>

Once the application is run the page only displays half the silverlight content in IE and Firefox doesn't display the silverlight content at all.  I immediately went to google and found this post with a solution for fixing the problem in firefox.  The solution it offers is to remove teh doctype declaration from the page.  Although doing this does fix firefox it causes IE to not display the silverlight content at all.  Again, Chrome seems to come out on top and still displays silverlight as expected.
Having said all this the actual solution was pretty straightforward, the problem is the height style in the silverlighht style sheet does not have the same affect as when using the single sample page previously.

   1:  #silverlightControlHost 
   2:  {
   3:      height: 100%;
   4:      text-align:center;
   5:  }

I simply changed the height to use a fixed size as below.

   1:  #silverlightControlHost 
   2:   {
   3:       height:550px;
   4:       text-align:center;
   5:   }

I would like to say there was more to it than that but that is it.  the silverlight content now displays correctly in ll IE, Firefox and Chrome.

Update:

if you  don't want to specify a fixed size affecting all your silverlight pages you could specify a height on each opening silverlight div.  Here I have specified a fixed height of 525 pixels which will override the stylesheet.

   1:    <div id="silverlightControlHost" style="height:525px" >
   2:          <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
   3:            <param name="source" value="ClientBin/WebcamMic.xap"/>
   4:            <param name="onError" value="onSilverlightError" />
   5:            <param name="background" value="white" />
   6:            <param name="minRuntimeVersion" value="4.0.41019.0" />
   7:            <param name="autoUpgrade" value="true" />
   8:            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.41019.0" style="text-decoration:none">
   9:                 <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
  10:            </a>
  11:          </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>


Perpetual Rehabilitation Site Launch

I just wanted to let you know about a new website I helped a friend set up.

http://www.perpetualrehabilitation.org

It's not an uber flashy site but serves an important servie.

 

Perpetual Rehabilitation Fund is a charity to assist individuals in funding their rehabilitation from addictions.  The success of the charity is heavily dependant on donations.

Please take a minute to visit and look around.


Sorting Listview on Multiple Columns

You can download the demo application with detailed comments from here ListViewSortDemo.zip (47.56 kb)

A little while ago I was working on a project where I needed to display the contents of a given folder. When sorting the columns it was requested that the application behaved similar to windows explorer. Sorting on the name column in ascending order would group the folders followed by the files, and sorting in descending order would list the files first then folders.

Ascending Sorting in Windows Explorer
Ascending Sorting in Windows Explorer

 Descending Sorting in Windows Explorer
Descending Sorting in Windows Explorer

I realised it was worth investing some time in creating a reusable lisvtiew sorting class which can sort multiple columns. 

Sorting a listview can be achieved by creating a class that inherits from the ICompare interface.  I'm not going to go into the technicalities of ICompare here, rather I am going to present the features of the class I have written.  It is sufficient to say though that the comparison of string values is not case sensitive, so 'hello world' will be considered the same as 'HeLlO WorLd'. 

While looking through the code below bear in mind that this is only one example of how it can be done, the code is open to scrutiny and opinion of all.

Listiview Sorter Features

Multiple column sort

A sort can be performed containing upto three columns (personally I have not yet found the need to sort on more than a couple of columns as yet).  This is achieved by three properties;   SortAppendColumn, SortColumn and SortPrependColumn. 

  • SortColumn.  This is column clicked on the listview by the user.
  • SortAppendColumn.  When a column index is specified for the SortAppendColumn the listview is sorted by this column before the actual clicked column.  An index of -1 indicates no column
  • SortPrependColumn.  When a column index is specified for the SortPrependColumn the listview is sorted by this column after the actual clicked column.  An index of -1 indicates no column

So the idea is, if the user clicks on the header of column 1 you can specify a sortprependcolumn, for example column 3.  What the class will do while sorting is simply append the contents of the relevant row in column 3 to column 1 before a compare.  This is assuming a textual comparison is being made.  This is all made clearer in the accompanying demo application.

For those of you more interested in the technical here is the code from the Compare method that analyses the properties and sets up the text to be compared;

   1:          ListViewItem listviewX, listviewY;
   2:   
   3:          // Cast the  objects to be compared to ListViewItem objects
   4:          listviewX =  (ListViewItem)x;
   5:          listviewY = (ListViewItem)y;
   6:   
   7:          StringBuilder XCompare = new StringBuilder();
   8:          StringBuilder  YCompare = new StringBuilder(); 
   9:   
  10:          // Compare the two  items
  11:          if (this.SortPrependColumn> -1 )
  12:          {
  13:               XCompare.Append(listviewX.SubItems[this.SortPrependColumn].Text);
  14:               YCompare.Append(listviewY.SubItems[this.SortPrependColumn].Text); 
  15:           }
  16:   
  17:          XCompare.Append  (listviewX.SubItems[MainSortColumn].Text);
  18:           YCompare.Append(listviewY.SubItems[MainSortColumn].Text);
  19:   
  20:           if (this.SortAppendColumn > -1)
  21:          {
  22:               XCompare.Append(listviewX.SubItems[this.SortAppendColumn].Text);
  23:               YCompare.Append(listviewY.SubItems[this.SortAppendColumn].Text); 
  24:           }

Textual or Numeric Comparison

When numbers are sorted in text format the result looks something like this;

1
14
2
26
3
37
4
...

Some developers tend to work around this feature by padding the number to a specified length with zeros like so;

0001
0002
0010
0014
...

and putting the padded numbers into an extra hidden column.  When the column containing the original numbers is clicked, the sorting is actually performed on the hidden column.

One of the properties you can set on the sorter class is whether a textual or numeric sort should be performed.  A numeric sort will avoid the above issues and sort number values in the expected logical way.  This was actually a new feature I implemented while writing this article, you never stop learning. 

Using the Class

At the top of your form declare an instance of the ListviewColumnSorter class.

   1:  ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();

Inside your form constructor assign the class to the listview.

   1:      // Assign the column sorter
   2:      listView1.ListViewItemSorter = lvwColumnSorter;

In the ColumnClick event set the class properties and call the listview sort method.
First we check if we are changing the sort order on the previously selected column or on a different column.
Next, if the clicked column is index 2 we tell it to sort on column index 3 then column index 2. This mimics the windows explorer functionality mentioned earlier of grouping the folders and files when sorting.
Lastly, if column index 4 was clicked then we are sorting a number column so the compare type is set to numeric.

   1:  private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
   2:   
   3:   
   4:  {
   5:      // Determine if clicked column is already the column that is being sorted.
   6:      if (e.Column == lvwColumnSorter.SortColumn)
   7:      {
   8:          // Reverse the current sort direction for this column.
   9:          if (lvwColumnSorter.Order == SortOrder.Ascending)
  10:          {
  11:              lvwColumnSorter.Order = SortOrder.Descending;
  12:          }
  13:          else
  14:          {
  15:              lvwColumnSorter.Order = SortOrder.Ascending;
  16:          }
  17:      }
  18:      else
  19:      {
  20:          // Set the column number that is to be sorted; default to ascending.
  21:          lvwColumnSorter.SortColumn = e.Column;
  22:          lvwColumnSorter.Order = SortOrder.Ascending;
  23:      }
  24:      
  25:      if (lvwColumnSorter.SortColumn == 2)
  26:          lvwColumnSorter.SortPrependColumn = 3;
  27:             
  28:      if (lvwColumnSorter.SortColumn == 4)
  29:          lvwColumnSorter.CompareAs = SorterCompareType.Numeric;
  30:      else
  31:          lvwColumnSorter.CompareAs = SorterCompareType.Textual ;
  32:   
  33:      // Perform the sort with these new sort options.
  34:      listView1.Sort();
  35:  }

You can download the demo application with detailed comments from here ListViewSortDemo.zip (47.56 kb)


File Download Counter Blogengine Extension

I like the idea of tracking how many times files have been downloaded from the site.  There is a sence of achievement as you see the count increase over a period of time.
Over the past couple of years I have come across a few good file download extensions written for blogengine.

Al Nyveldt – SimpleDownloadCounter

Ruslan Tur – DownloadCounter

Chris Blakenship - FileDownloadCounter

The extension by Chris has been installed on my site for the last year or two and it has worked very well.  I have recently come to a point where I need, or rather want, more features than the extension provides.  I had two options, to write my own from scratch, or enhance an already written extension to include the features I require.  I decided stick with the filedownloadcounter by Chris Blakenship as it was already integrated in to the site and it would be a good foundation to start from.

The features I decided to build into to extension are:

  • Record the date the file was last downloaded.
  • The ability to add a description for the file.
  • The ability to assign the file to a category e.g. theme, extension, application.
  • When a file is uploaded it goes into a sub folder off of the standard files folder.  The sub folder is constructed from the current year and month, e.g. yourdomain.com/files/2010/5. I wanted a way to record this location for each file.

To accomodate for these changes the xml file structure has chanded:

   1:  <?xml version="1.0" encoding="utf-8" standalone="yes"?>
   2:  <files>
   3:      <file name="archive.zip" 
   4:          path="2008/3/archive.zip" dateadded="03/03/2008 18:33:30" 
   5:          description="This extension displays a random quote." 
   6:          lastdownload="5/31/2010 5:37:28 PM" 
   7:          category="Blogengine Extensions" 
   8:          ipaddress="10.10.10.10">
   9:       207
  10:   </file>
  11:  </files>

Installation

  1. Copy the files from admin-pages folder to your site admin/pages folder.
  2. Copy the files from app_code-controls folder to your site app_code/controls folder.
  3. Copy the files from app_code-extensions folder to our site app_code/extensions folder.
  4. Copy the usercontrol from the user controls folder to your site User Controls folder.

The admin page allows you to edit the the description and the category, the latter not being case sensitive. The admin page also has a file upload facility with an option where you can choose whether or not to create an entry in the xml file.

Dislpaying the Download Count

There are two ways to display the download count.  This is the first way;

   1:  <div class="box">
   2:      <h1>File Downloads</h1>
   3:      <blog:DownloadedFileStats ID="DownloadedFileStats" runat="server"  />
   4:  </div>

The second way is to use the usercontrol in a blogengine page, standard aspx page or similar by usercontrol injection.
NOTE: do not include any spces.

[ usercontrol: ~/user controls/downloadedfilestats.ascx ]

the results are displayed in a table which has classes assigned to its elements for styling.

CountFileDownloadsTable - table level
CountFileDownloadsTableTH - Table header
CountFileDownloadsTableTR - Table row
CountFileDownloadsTableTD - Table data
CountFileDownloadsTableCategory - Category row

 as I have done here;

The Download File Counter can be downloaded from here DownloadedFileCounter.zip (10.07 kb)
and my download page.


site and blogengine upgrade

I haven't been very active with blogging for a while, as you can probably tell from the lack of posts.  I plan to be more active, so to give things a kick start the site has had a small facelift.  I was running the Leaves theme unmodified.  To spruce things up I have:

  • Added some colour, mainly to the haeder. 
  • The theme also now supports multiple widgetzones, one on the left sidebar and one on the right sidebar.
  • The site menu has been moved from the left sidebarto the header.

I have also upgraded the blogengine to version 1.6.1.0.  the upgrade went straight forward.  I am working on updating the randomquotes extension and a modified version of the filedownloadcounter extension. This brings me on to a request and would really appreciate you doing the following.

  • If you have downloaded and are using any of the themes or extensions from this site please let us know, we will be creating a directory of sites users.
  • Also, if you have any suggestions for extensions or enhancements to the current extensions please let us know, it will help us to help you.

Thats it for now.


BlogEngine 1.5 Release Candidate

A couple of weeks ago the BlogEngine team announced the release of version 1.5 RC (Release Candidate).  This motivated me to do a number of things

  1. Download and install run 1.5 RC locally to see what has changed.
  2. Document the changes I have made to this site, extension installations, page modifications etc.
  3. Install these modifications to 1.5 RC to check their compatability in preparation for a future upgrade to 1.5 when the final version is released.
  4. Finally the main reason for playing with this newest release was to test the themes and extensions I have written that you have downloaded.  At the moment that is only the Greenway theme and Randomquote extension.

I am glad to report that RandomQuote version 1.2 still works fine and is compatible with 1.5 RC.  Hopefully nothing will change between the RC and Final versions to change that. 

Unfortunately there does seem to be a little bug, or should I call it a special feature, in the Greenway theme on some browsers.  The theme displays as expected in IE7

Greenway Theme in IE 7 
The calendar and RandomQuote extension are nicely positioned under the menu bar. 
Now compare that with how it looks in FireFox 3.0.9 

Greenway Theme in Firefox 3.0.9

You can clearly see the difference in the position of the calendar and RandomQuote extension.  I don't believe this is a direct affect of version 1.5, this bug has probably always been there.  Due to its nature I am not going to urgently look into fixing it, sorry guys, personal commitments prevent me from spending the necessary time on it.  I know what you are thinking, you can spend time writing about it so why not spend some of that time fixing it.  Fair argument I suppose.  If any of you have found a way to resolve the issue feel free to get in touch.

For more information regarding 1.5 see the announcement on the official BlogEngine site

As usual you can get all the up to date downloads from the downloads page.


Silverlight - This weeks 7 day wonder

I love learning new stuff, which causes many hours of internet surfing when I should really be doing something else.  My mum used to call them 7 day wonders.

This weeks big thing for me is Silverlight 2.  I have been looking for tutorials on writing games using Silverlight and found some very interesting sites.  Here are a few that I found did focus on games.

The first one is Bluerosegames.com.  If you search for 'Silverlight game tutorial' on google you will currently find it at the no. 1 position.  There is a series of tutorials as listed below

I have read through the bluerosegames tutorials and highly recommend them.  They are written in C# and focus on object oriented methods.  During the tutorials the author takes you through the beginnings of creating an asteriod game.  Excellent for getting you started.

dieAjax have a mini series on creating a shooter game.

For more general Silverlight  tutorials I recommend visiting Microsoft's own Silverlight.net. or searching google for pages of lists of tutorials.


Old Flash Banners

I was doing some digging around in some old folders in a corner of my hard drive I have not used for quite some time and came across these banners I had made some time ago.  

I created this one totally from scratch.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player



I created this one for a challenge being held on Keyframer.com.  Some of the assets were provided by them.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Creating these banners was really the first time I had used flash for animation.


RandomQuote 1.2 Update

As promised the RandomQuote extension to DNBE is complete and has been uploaded to this site.
The current version is RandomQuote 1.2.
I do not plan on any major changes or updates to the extension, that is unless someone finds a major bug.

There were a couple of bugs fixed, nothing big.  As usual you can get the updated files from my downloads page.
Thankyou all for taking the time to use this extension. 
If you have any comments or suggestions please get in touch, I would be very intrested to hear if you are using RandomQuote and what you think of it.