Solved

Passing a listview object

Posted on 2014-02-19
7
364 Views
Last Modified: 2014-02-20
Experts, I have a project with many forms and listviews. In a database i want to store the column widths when the form closes and set those widths when the form opens. This is so the listview columns stay the way the user set them. Currently i have a procedure when the form loads called loadColumns and a procedure when the form closes called saveColumns. I was wondering how i could put these procedures in a module and pass the listview to the procedure in the module. That way i wont have to put these procedures in all the forms. I think i can see how to pass the listview to the saveColumns procedure and get the column widths but i have no idea how to do it with the loadColumns procedure.

Can you show me how I could do this?
0
Comment
Question by:Basicfarmer
  • 4
  • 3
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39872533
You presumably have a procedure something like:
sub SaveView()
here you save the view
end sub

now make a procedure
sub SaveView(lv as ListView, sID as string)
now instead of hard coding the listview name use lv and use the sID as the identifier of the listview for in the table
end sub
and call it SaveView(listView1, "listView1"), SaveView(listView456, "listView456).....

Use the same for loading
0
 

Author Comment

by:Basicfarmer
ID: 39874054
OK i have a good procedure for saving the columns that can be used from all of my forms. But i cannot understand how to do a load procedure to load the column widths to the listview on a form.

    Public Sub saveColumns(lstName As String, lstColumn As ListView.ColumnHeaderCollection)

        Dim strSql As String, cmd As OleDbCommand, oHeader As ColumnHeader, cmdStr As String

        cmdStr = vbNullString

        For Each oHeader In lstColumn

            cmdStr = cmdStr & "C" & oHeader.Index & " = " & oHeader.Width & ","

        Next

        'Remove the last comma.
        cmdStr = Left(cmdStr, Len(cmdStr) - 1)

        strSql = "UPDATE Columns SET " & cmdStr & " WHERE ListView = '" & lstName & "'"

        cmd = New OleDbCommand(strSql, con)
        cmd.ExecuteNonQuery()
        cmd.Dispose()

    End Sub

Open in new window

0
 

Author Comment

by:Basicfarmer
ID: 39874189
Here is where i am stuck trying to load column widths. I dont know how to reference the columns. I may need to change the table in the database somehow. I guess it is the indexing that I dont know what to do with. I am also unsure of the way i am passing the listview.

The Columns table in the database looks like this:

Listview (Name of listView)
C0 (Column To Store)
C1
C2
C3...

    Public Sub loadColumns(frm As Form, lstView As ListView)

        Dim strSql As String, cmd As OleDbCommand, reader As OleDbDataReader, oColumn As ColumnHeader

        strSql = "SELECT * FROM Columns WHERE ListView = '" & frm.Name & "_" & lstView.Name & "'"

        cmd = New OleDbCommand(strSql, con)
        reader = cmd.ExecuteReader()

        For Each oColumn In lstView.Columns

            reader.Read()
            lstView.Columns(

        Next

    End Sub

Open in new window

    Public Sub saveColumns(lstName As String, lstColumn As ListView.ColumnHeaderCollection)

        Dim strSql As String, cmd As OleDbCommand, oHeader As ColumnHeader, cmdStr As String

        cmdStr = vbNullString

        For Each oHeader In lstColumn

            cmdStr = cmdStr & "C" & oHeader.Index & " = " & oHeader.Width & ","

        Next

        'Remove the last comma.
        cmdStr = Left(cmdStr, Len(cmdStr) - 1)

        strSql = "UPDATE Columns SET " & cmdStr & " WHERE ListView = '" & lstName & "'"

        cmd = New OleDbCommand(strSql, con)
        cmd.ExecuteNonQuery()
        cmd.Dispose()

    End Sub

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39874242
When you save you just write one string  with all the information into the table.
So, when you lad you just get one string back then inside the looping through the columns you take that string apart to determine the widths of each column.
0
 

Author Comment

by:Basicfarmer
ID: 39874288
Ok, got it. Seems to be working just like i want. One quick question kinda off topic. I am trying to dispose of the oColumn object. But i get this warning. How can i dispose of this object properly.

"Warning      1      Variable 'oColumn' is used before it has been assigned a value. A null reference exception could result at runtime.      C:\Users\blaju\Documents\My Projects\QuoteMaster\Project\QuoteMaster\QuoteMaster\systemFunctions.vb      69      9      QuoteMaster"

I have tried:
If oColumn is nothing then oColumn.Dispose

    Public Sub loadColumns(frm As Form, lstView As ListView)

        Dim strSql As String, cmd As OleDbCommand, reader As OleDbDataReader, oColumn As ColumnHeader

        strSql = "SELECT * FROM Columns WHERE ListView = '" & frm.Name & "_" & lstView.Name & "'"

        cmd = New OleDbCommand(strSql, con)
        reader = cmd.ExecuteReader()

        For Each oColumn In lstView.Columns

            reader.Read()
            lstView.Columns(oColumn.Index).Width = CInt(reader.Item("C" & oColumn.Index))

        Next

        oColumn.Dispose() <-- RIGHT HERE HOW TO DISPOSE

        reader.Close()
        cmd.Dispose()

    End Sub

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39874401
I'd just set it to null and let the GC handle it.
0
 

Author Closing Comment

by:Basicfarmer
ID: 39874433
Nevermind about the disposing it disposes of one of my columns. I have everything working correctly now. Thanks for help...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now