Solved

Passing a listview object

Posted on 2014-02-19
7
363 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 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

19 Experts available now in Live!

Get 1:1 Help Now