Monday, 6 June 2011

How to sort listview by clicked column


Found an excellent article on another blog http://www.fryan0911.com/2009/05/vbnet-how-to-sort-listview-by-clicked.html I'll also post the article here in case Fryan's blog is ever down

 1. On your existing project, add a new class with following code:

Public Class clsListviewSorter ' Implements a comparer
    Implements IComparer
    Private m_ColumnNumber As Integer
    Private m_SortOrder As SortOrder
    Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
        m_ColumnNumber = column_number
        m_SortOrder = sort_order
    End Sub
    ' Compare the items in the appropriate column
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
        Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
        ' Get the sub-item values.
        Dim string_x As String
        If item_x.SubItems.Count <= m_ColumnNumber Then
            string_x = ""
        Else
            string_x = item_x.SubItems(m_ColumnNumber).Text
        End If
        Dim string_y As String
        If item_y.SubItems.Count <= m_ColumnNumber Then
            string_y = ""
        Else
            string_y = item_y.SubItems(m_ColumnNumber).Text
        End If
        ' Compare them.
        If m_SortOrder = SortOrder.Ascending Then
            If IsNumeric(string_x) And IsNumeric(string_y) Then
                Return Val(string_x).CompareTo(Val(string_y))
            ElseIf IsDate(string_x) And IsDate(string_y) Then
                Return DateTime.Parse(string_x).CompareTo(DateTime.Parse(string_y))
            Else
                Return String.Compare(string_x, string_y)
            End If
        Else
            If IsNumeric(string_x) And IsNumeric(string_y) Then
                Return Val(string_y).CompareTo(Val(string_x))
            ElseIf IsDate(string_x) And IsDate(string_y) Then
                Return DateTime.Parse(string_y).CompareTo(DateTime.Parse(string_x))
            Else
                Return String.Compare(string_y, string_x)
            End If
        End If
    End Function
End Class

2. Declare a private variable on the form where the listview you want to be sorted is located.

 Private m_SortingColumn As ColumnHeader

3. Then on the listview's ColumnClick event, add the following code

 Private Sub ListView1_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        ' Get the new sorting column.
        Dim new_sorting_column As ColumnHeader = ListView1.Columns(e.Column)
        ' Figure out the new sorting order.
        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            ' New column. Sort ascending.
            sort_order = SortOrder.Ascending
        Else ' See if this is the same column.
            If new_sorting_column.Equals(m_SortingColumn) Then
                ' Same column. Switch the sort order.
                If m_SortingColumn.Text.StartsWith("> ") Then
                    sort_order = SortOrder.Descending
                Else
                    sort_order = SortOrder.Ascending
                End If
            Else
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            End If
            ' Remove the old sort indicator.
            m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
        End If
        ' Display the new sort order.
        m_SortingColumn = new_sorting_column
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.Text = "> " & m_SortingColumn.Text
        Else
            m_SortingColumn.Text = "< " & m_SortingColumn.Text
        End If
        ' Create a comparer.
        ListView1.ListViewItemSorter = New clsListviewSorter(e.Column, sort_order)
        ' Sort.
        ListView1.Sort()
    End Sub

Tuesday, 3 May 2011

PHPBB3 Tables and Excel

One of the forums I currently host and manage makes use of PHPBB3, the user’s post a lot of different tables (league standings e.t.c.) so being able to post tables is a must so I added some BB codes for [table], [tr] and [td] and they can now happily post all the tables they like.

The source data for these tables is held in excel, and having to upload and update 3 different tables each week is a little tiresome so I developed a little VBA Macro that will convert a selection to PHPBB3 Table code.

Throw a button on the toolbar and link it to the macro and job done!



Private Sub CommandButtonClose_Click()
    'closes the form
    End
End Sub

Private Sub CommandButtonCopy_Click()
    'Copies the content of the TextBoxTable
    Dim ansDataO As DataObject
    Set ansDataO = New DataObject
   
    ansDataO.SetText TextBoxTable.Text
    ansDataO.PutInClipboard
End Sub



Private Sub UserForm_Initialize()
    Dim i_ColumnCount As Integer
    Dim i_RowCount As Integer
    Dim i_ColumnLoop As Integer
    Dim i_RowLoop As Integer
   
    'Gets the number of columns / rows in our selection
    i_ColumnCount = Selection.Columns.Count
    i_RowCount = Selection.Rows.Count
 
    'stores the phpbb code
    TextBoxTable.Text = "[table]"
   
    'loops through each row in our selection
    For i_RowLoop = 1 To i_RowCount
        TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[tr]"
           
        'for each row we loop, now loop through the column
        For i_ColumnLoop = 1 To i_ColumnCount

            TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[td]"
           
            TextBoxTable.Text = TextBoxTable.Text & Selection.CurrentRegion.Cells(i_RowLoop, i_ColumnLoop).Value
                   
            TextBoxTable.Text = TextBoxTable.Text & "[/td]"
        Next

        TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[/tr]"
    Next
       
    TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[/table]"

End Sub


Thursday, 10 February 2011

PHP IIS Got a whole lost easier

I can't remember if I’ve ever blogged about my problems setting PHP up on Windows under IIS, it can be a real headache... sometimes it works as per the documentation, other times (even on a clean server) it doesn't and I end up messing around with all kinds of settings and NTFS permissions, after several system reboots things finally start to work.

Recently I’ve been working on a project that involves using PHP and MSSQL (something I've not done before) it looks like php_mssql.dll is now history (up to PHP v5.2) the latest version of PHP uses a slightly different dll.

To my relief my latest PHP installation was a breeze, fire up the URL below, check all the stuff you want it to install and off it goes, all configured, all working (I wonder what the security is like)

http://www.microsoft.com/web/Downloads/platform.aspx

Wednesday, 19 January 2011

Online document conversion

I needed to convert a PDF document to an image today (didn't want to screen dump it due to resolution loss) after finding 2 or 3 services that boasted they could do the job 'online' and for 'free' (all 3 of which failed or couldn't handle a PDF as large as 1MB) I came across Zamzar. This website can handle a lot of common file conversions (e.g. mov to mpeg e.t.c.) It also features a YouTube video ripper (that could come in handy)

Check it out at http://www.zamzar.com/

Monday, 17 January 2011

Working with PDF’s


Occasionally I get asked to edit and amend some PDF documents, we only have 2 full Adobe Acrobat licenses on site and I don’t have access to one. I can create PDF documents pretty easily from Word and Excel 2007 by using a Microsoft Office Add-in (available here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=4d951911-3e7e-4ae6-b059-a2e79ed87041&displaylang=en) I can also produce PDF documents from any application that allows you to print by using CutePDF, which installs itself as a printer (available here: http://www.cutepdf.com/)

I few years ago I needed to actually reorder and edit some documents I found the PDF Toolkit Builder (PDFTK) immensely helpful as it allows you to collate and / or split existing PDF documents (you can also stamp and even rotate existing documents) check out PDFTK Builder (available here: http://portableapps.com/apps/office/pdftk_builder_portable)

Thursday, 4 November 2010

None of your e-mail accounts could send to this recipient


Ok I had a very strange email issue today. One of my clients tried sending to various email addresses and kept getting a bounce message with “None of your e-mail accounts could send to this recipient” error. 

After some investigation a number of contacts had been setup to use “MAILTO” rather than “SMTP” (you can check this by double clicking on the contacts name) to resolve the issue I deleted the contact and typed them in, then doubled clicked on them to verify that it had defaulted to SMTP.

Tuesday, 17 August 2010

Form_Load and Textbox.Focus()

I was working on a project recently, where I had MDI forms with SplitContainers and I came across an annoying issue where the Split Containers split bar had the focus, and it made the form look really ugly, so upon load I wanted to set the focus to a textbox, however the normal method of Textbox.Focus() doesn’t seem to work when used inside the Form_Load event, the solution to this is rather simple

Me.ActiveControl = Me.Textbox