Sunday, August 17, 2008

The Joy of computing and Microsoft

I needed to swap two columns in a listview. No big deal, one line of code:

listView1.Columns(1).DisplayIndex = 0

Well. That was with Microsoft's .NET Framework 2.0. I needed Framework 1.1. In 1.1 I had to figure out a way to do it. Microsoft did not provide a way. Not a lot of code but low level and not obvious:



Public Declare Function SendMessagelong Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Integer, ByVal wMsg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const LVM_FIRST = &H1000
Private Const LVM_GETCOLUMNORDERARRAY = (LVM_FIRST + 59)
Private Const LVM_SETCOLUMNORDERARRAY = (LVM_FIRST + 58)

Dim success As Long
Dim i As Integer
Dim colCount As Long
Dim colArray(2) As Long

colCount = Me.listView1.Columns.Count
ReDim colArray(0,colCount) As Long
success = SendMessage(Me.Handle.ToInt32, LVM_GETCOLUMNORDERARRAY, colCount, colArray(0))



If success <> 0 Then

colArray(0) = 1
colArray(1) = 2
colArray(2) = 3


success = SendMessage(Me.listView1.Handle.ToInt32, LVM_SETCOLUMNORDERARRAY, colCount, colArray(0))

Me.listView1.Refresh()
End If