Need help with columnorder saving class

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I found a class that saves column order and column width for a list view but i cant get it to work properly.
Found it here!
http://www.dotnet247.com/247reference/msgs/11/56529.aspx http://www.dotnet247.com/247reference/msgs/11/56529.aspx
<pre class="prettyprint lang-vb Public Class cListviewHelper
Private Declare Function ListViewiColumnOrder Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer

Passing the form name avoids errors if same listview name is used on multiple forms.
Specail code should be added if column counts change

Change This To The Name Of Your App.
Private Shared m_sAppName As String = "RRO"

Public Shared Sub Save(ByVal sFormName As String, ByVal oListView As ListView)
Call SaveColumnWidthsToReg(sFormName, oListView)

Dim iColumnOrder() As Int32 = GetiColumnOrderArrayFromListView(oListView)

If Not IsNothing(iColumnOrder) Then
Call SaveColumnOrderArrayToReg(sFormName, oListView, iColumnOrder)
End If
End Sub

Public Shared Sub Restore(ByVal sFormName As String, ByVal oListView As ListView)
Call SetListViewColumnWidths(sFormName, oListView)

Dim iColumnOrder() As Int32 = GetiColumnOrderArrayFromReg(sFormName, oListView)

If IsNothing(iColumnOrder) = False Then
Call SetListViewiColumnOrder(oListView, iColumnOrder)
End If
End Sub

Private Shared Sub SaveColumnOrderArrayToReg(ByVal sFormName As String, ByVal oListView As ListView, ByVal iColumnOrder() As Int32)
Dim iLP As Int32

Dim sSection As String = sFormName & "~" & oListView.Name

Dim sWidths As String
For iLP = 0 To iColumnOrder.Length - 1
sWidths = sWidths & iLP

If iLP <> iColumnOrder.Length - 1 Then
sWidths = sWidths & ","
End If
Next

SaveSetting(m_sAppName, sSection, "ColumnOrder", sWidths)

End Sub

Private Shared Function GetiColumnOrderArrayFromReg(ByVal sFormName As String, ByVal oListView As ListView) As Int32()

Dim sSection As String = sFormName & "~" & oListView.Name
Dim sOrder As String = GetSetting(m_sAppName, sSection, "ColumnOrder", "Missing")

If sOrder = "Missing" Then
Return Nothing
Else
Dim sArray() As String = Split(sOrder, ",")
Dim iLP As Int32
Dim iOut(sArray.Length - 1) As Int32

For iLP = 0 To sArray.Length - 1
iOut(iLP) = CInt(sArray(iLP))
Next

Return iOut
End If
End Function

Private Shared Function GetiColumnOrderArrayFromListView(ByVal oListView As ListView) As Integer()
Dim iGetiColumnOrderArray As Integer = (&H1000S + 59)
Dim iSuccess As Integer
Dim iSortOrder As Integer()
ReDim iSortOrder(oListView.Columns.Count - 1)
iSuccess = ListViewiColumnOrder(oListView.Handle.ToInt32, iGetiColumnOrderArray, iSortOrder.Length, iSortOrder(0))
If Not iSuccess.Equals(0) Then
Return iSortOrder
Else
Throw New Exception("Could not get the column order for listview " & oListView.Name)
End If
End Function

Private Shared Sub SaveColumnWidthsToReg(ByVal sFormName As String, ByVal oListView As ListView)
Dim oColumnHeader As ColumnHeader

For Each oColumnHeader In oListView.Columns

Dim sSection As String = sFormName & "~" & oListView.Name & "~" & "ColumnWidths"
Dim sWidth As String = oColumnHeader.Width.ToString
SaveSetting(m_sAppName, sSection, oColumnHeader.Index.ToString, sWidth)
Next
End Sub

Private Shared Sub SetListViewColumnWidths(ByVal sFormName As String, ByVal oListView As ListView)
Dim oColumnHeader As ColumnHeader

For Each oColumnHeader In oListView.Columns

Dim sSection As String = sFormName & "~" & oListView.Name & "~" & "ColumnWidths"
Dim sWidth As String = GetSetting(m_sAppName, sSection, oColumnHeader.Index.ToString, "60")

oColumnHeader.Width = CInt(sWidth)
Next
End Sub

Private Shared Sub SetListViewiColumnOrder(ByVal oListView As ListView, ByVal iColumnOrder As Integer())
If iColumnOrder.Length < 1 Then Exit Sub
If iColumnOrder.Length > oListView.Columns.Count Then Exit Sub
Dim iSetiColumnOrderArray As Integer = (&H1000S + 58)
Dim iSuccess As Integer
iSuccess = ListViewiColumnOrder(oListView.Handle.ToInt32, iSetiColumnOrderArray, iColumnOrder.Length, iColumnOrder(0))
If Not iSuccess.Equals(0) Then
oListView.Refresh()
End If
End Sub

End Class[/code]
I think the problem lays in this section!<br/>
Private Declare Function ListViewiColumnOrder Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer,
ByRef lParam As Integer ) As Integer
It saves the right number of columns but not in the order they are.
It always saves them like this in the registry "0,1,2,3,4,5,6"
Please help!

View the full article
 
Back
Top