?
Solved

How do I add items and subitems to a programtically created listview?

Posted on 2007-08-10
6
Medium Priority
?
447 Views
Last Modified: 2013-11-17
USING: VB.Net 2005 compact framework 2

I have a tab called tabProj and a page in that tab named tpTally. The code reads a database and adds textboxes and one listview depending on the database. The listview name is lvTree, that never changes but the number of columns changes based on the database.

There is a direct correlation between the textboxes and the subitems. When a button is pressed I want it to scan all textboxes in tpTally and then add the text in each textbox as a subitem in lvTree. The item will just be a counter.

For example the first time the item will be 1 (in other words the count of items in the listview + 1) and the subitems will be the value in each textbox.

I have the code working up to the point of adding all of the column names correctly. I just don't know how to add subitems to a listview that will be created at runtime.

Here is what I have and I don't even know if I am on the right track yet:

button click code: (not working)
        'add tree data to listview
        Dim tabpagADD As TabPage
        Dim lsi As New ListViewItem
        Dim lv As ListView
        For Each tabpagADD In tabProj.TabPages
            If tabpagADD.Name = "tpTally" Then
                For Each ctrlList As Control In tabpagADD.Controls
                    If TypeOf ctrlList Is ListView And ctrlList.Name = "lvTree" Then
                        For Each ctrlADD As Control In tabpagADD.Controls
                            If TypeOf ctrlADD Is TextBox And ctrlADD.Visible = True Then
                                        'add items and subitems here
                            End If
                        Next
                    End If
                Next
            End If
        Next

Any and all help is appreciated
0
Comment
Question by:PBLack
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19672735
Example:

    Dim lv As New ListView
    Dim item As ListViewItem = lv.Items.Add("Item")
    item.SubItems.Add("SubItem #1")
    item.SubItems.Add("SubItem #2")

Bob
0
 

Author Comment

by:PBLack
ID: 19672781
Hi

There is a problem with this solution on line

    Dim item As ListViewItem = lv.Items.Add("Item")

An error is thrown as follows

Value of type 'string' can not be converted to listview

a compact framework thing perhaps?

PBLack


0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19672873
Sorry, that would be:

    Dim lv As New ListView
    Dim item As New ListViewItem("Item")
    item.SubItems.Add("SubItem #1")
    item.SubItems.Add("SubItem #2")
    lv.Items.Add(item)

Bob


0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:PBLack
ID: 19672913
Ok here is what I have tried but there is an error thrown: see beside offending line below

        Dim lvTree As New ListView
        Dim Xadd As Integer

        Xadd = lvTree.Items.Count + 1

        Dim listItem As ListViewItem = New ListViewItem(Xadd.ToString)
        lvTree.Items.Add(listItem)

        For Each tabpag In tabProj.TabPages
            If tabpag.Name = "tpTally" Then
                For Each ctrl As Control In tabpag.Controls
                    If TypeOf ctrl Is TextBox And ctrl.Visible = True Then

                        listItem.SubItems.Add(ctrl.Text) - ERROR: Specified argument was out of the range of valid values. Parameter name: index

                    End If
                Next
            End If
        Next


        MsgBox("Done")
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19672927
I thought you said that you created columns for the ListView?  Maybe I misunderstood :(

Bob
0
 

Author Comment

by:PBLack
ID: 19672964
The columns are created programmatically. Anyway You have put me on the right path. I will mess with it and if I need more help I will post a follow-up question

Thanks for the help

PBLack
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 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