Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing a listview object

Posted on 2014-02-19
7
Medium Priority
?
377 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 45

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 45

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 45

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

610 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