I have several listviews in my VB6 application that are filled with data from an Access database. I have the "AllowColumnHeaderReorder" value set to true. This allows me to change the arrangement/order of the Columns and display the information, but it does not save this order for future views. Each time a new set of data is displayed, I have to rearrange the columns again.
Is there sample source code available that would allow a user to select what columns are displayed, in a listview, and in what order, and have those selections saved to an ini file, or someplace else?
The VB6 "Reference Listview" is an example of what I'd like to use to set these display settings. Then when the listview loads the data, it will be in the correct order.
Thank you for you response. But I was hoping for a reply with some code I could use as a beginning. Like your link, "VB - Color a row in a ListView".
With some source to examine, that covers how the form functions that I included in the attached illustration, I might be able to work out a solution for my needs.
I forgot to mention that all my changes are marked with 'NEW. Don't hesitate to ask if you have questions. When you have the answer you need in this thread you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that by selecting the Mark Thread Resolved item from the Thread Tools menu. Otherwise please insert "[Resolved]" at the start of the Subject and select the green checkmark from the Post Icons. Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help. BTW, no need to do it for me.
The information and code was a great start. I found an issue with the unload event, which is probably why you weren't happy with it. The code works fine the first time to save the re-ordered columns. But when you close the application the second time, the order reverts back to 123.
I know why this is happening, and I'm on the job of creating a solution. At least I have the basics to work with now. When I have something that works, I'll post it.
Private Sub Form_Unload(Cancel As Integer)
'NEW - Save the column order. (I'm not happy with this but it works.)
Dim strOrder As String
Dim intVal As Integer
intVal = 1
Do Until lvFind.ColumnHeaders(intVal).Position = 1
intVal = intVal + 1
Loop
strOrder = lvFind.ColumnHeaders(intVal).Index
intVal = 1
Do Until lvFind.ColumnHeaders(intVal).Position = 2
intVal = intVal + 1
Loop
strOrder = strOrder & lvFind.ColumnHeaders(intVal).Index
Private Sub Form_Load()
' Create the instance of the database class
Set gDB = New CDatabase
' Open the database
gDB.dbName = App.Path & "\northwind.MDB"
'NEW - Get saved order. If there are none (1st run) then the default order is 123
mstrColumnPositions = GetSetting("MyColumnPositionApp", _
"Column Positions", "MyListview", "Product|Supplier ID|Category ID")
mstrColumnPositions = Replace(mstrColumnPositions, " ", "_")
Dim strParts() As String
strParts = Split(mstrColumnPositions, "|")
CallByName frmFind, strParts(0), VbMethod
CallByName frmFind, strParts(1), VbMethod
CallByName frmFind, strParts(2), VbMethod
lvFind.BorderStyle = ccFixedSingle ' Set BorderStyle property.
lvFind.View = lvwReport ' Set View property to Report.
' Label OptionButton controls with SortOrder options.
OptSort(0).Caption = "Sort Ascending"
OptSort(1).Caption = "Sort Descending"
lvFind.SortOrder = lvwAscending ' Sort ascending.
OptSort(0).Value = True
End Sub
Replace the Column1, etc subs with these.
Code:
Public Sub Product()
Dim clmX As ColumnHeader
Set clmX = lvFind.ColumnHeaders.Add(, , "Product", (lvFind.Width - 1066) / 2)
End Sub
Public Sub Supplier_ID()
Dim clmX As ColumnHeader
Set clmX = lvFind.ColumnHeaders.Add(, , "Supplier ID", (lvFind.Width - 1066) / 2)
End Sub
Public Sub Category_ID()
Dim clmX As ColumnHeader
Set clmX = lvFind.ColumnHeaders.Add(, , "Category ID", 976)
End Sub
Code:
Private Sub Form_Unload(Cancel As Integer)
Dim strOrder As String
Dim intVal As Integer
SaveSetting "MyColumnPositionApp", "Column Positions", "MyListview", _
lvFind.ColumnHeaders(lvFind.ColumnHeaders(1).Position).Text & "|" & _
lvFind.ColumnHeaders(lvFind.ColumnHeaders(2).Position).Text & "|" & _
lvFind.ColumnHeaders(lvFind.ColumnHeaders(3).Position).Text
Set frmFind = Nothing
End Sub
BTW, your registry will contain 123 or some variation of that order and in my new scheme you use the column header text so you'll have to override what is there the first time after you make these above changes. Just in case you don't know how to do that
Put a breakpoint on the mstrColumnPositions = Replace(mstrColumnPositions, " ", "_") line
In the Immediate Window type mstrColumnPositions = "Product|Supplier ID|Category ID" and press return.