Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listview with two columns. I also have a textbox. In the textbox, there are lines with strings that I want to insert into the listview.
Every 1st line will be inserted into the first column and every 2nd line will be inserted into the second column. How would I achieve this.
This is my current code:
VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox3.Text = My.Resources.clsid
        Dim ListArr(230) As String
        Dim i As Integer = 0
        For Each line As String In TextBox3.Lines
            ListArr(i) = line(i)
            i += 1
            For Each litem As String In ListArr
                AddLitem(litem, litem)
            Next
        Next
    End Sub
    Public Function AddLitem(ByVal Desc As String, ByVal CLSID As String)
        Dim litem As ListViewItem
        If i = 0 Then
            Lstr(0) = Desc
            i += 1
        ElseIf i = 1 Then
            Lstr(1) = Desc
            litem = New ListViewItem(Lstr)
            ListView1.Items.Add(litem)
            ListView1.Items.RemoveAt(ListView1.Items.Count)
        End If
        Lstr(0) = Desc
        'Lstr(1) = CLSID
    End Function

Please help me.
Thanks in advance.
Posted

1 solution

Hi, i did not understand your question, but if you simply want to add values in to the ListView columns. Please refer below code:
This code simply read lines from text box and add into the ListView columns.
VB
Dim mTextBoxLines As String() = Me.TextBox1.Lines 'Read Lines From TextBox
Dim mLine As Int16 = 1
'Add First Line Into the First Column Of ListView
Dim mListViewItem As ListViewItem = New ListViewItem(mTextBoxLines(0))
While mLine <> mTextBoxLines.Count
   'Add Other Lines Into the Next Columns Of ListView
   mListViewItem.SubItems.Add(mTextBoxLines(mLine))
   mLine = mLine + 1
End While
Me.ListView1.Items.Add(mListViewItem)

I hope it will help you. :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Mar-15 1:31am    
Good enough. I also would not try to understand detail of the question; this code sample tells us enough.
Using while loop makes code less clear than for loop. I up-voted it with my 4.
—SA
Manoj K Bhoir 9-Mar-15 1:37am    
Thank you Sergey Alexandrovich Kryukov :)
iProgramIt 9-Mar-15 1:46am    
It will only put in the first item, and then it stops. Why?
Manoj K Bhoir 9-Mar-15 1:57am    
Please try to debug your code because above sample code is working properly.
iProgramIt 9-Mar-15 2:15am    
What Framework? Im using .net 4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900