?
Solved

Passing a listview object

Posted on 2014-02-19
7
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

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…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 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