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
Solved

Passing a listview object

Posted on 2014-02-19
7
368 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 42
Finding the IIS version 5 21
Name Space error VS2015 1 22
VB.net -  GroupBox / Panel DataBinding and behavior 3 12
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

809 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