hi guys just want to ask a simple question about listview

im making a POS program..

when i click Enter button the items on textbox and comboboxes should be placed inside my listview

here is my screenshot
http://i43.photobucket.com/albums/e355/bettybarnes/pos.jpg

my problem is i dont know how to put the correct codes for listview

any help please? thanks

Recommended Answers

All 6 Replies

See if this helps.

Dim lvItem As New ListViewItem(TextBox1.Text) '// declare new ListView item and set TextBox.Text as item.Text.
        With lvItem.SubItems
            .Add(ComboBox1.Text) '// SubItem 1 for Column 2.
            .Add(ComboBox2.Text) '// SubItem 2 for Column 3.
        End With
        ListView1.Items.Add(lvItem) '// add item to ListView.

hi thanks codeorder what if i want to cancel the transaction? it should also be cleared out(the items on listview)

hi i know that it Listview1.items.clear() is one of the best way to do this. do you have any other way to refresh or clear the items?

If you need to clear all the items, then ListView1.Items.Clear() is the appropriate way to do it.
.Although, you can use a For/Next loop to clear items as well.

With ListView1
            For i As Integer = .Items.Count - 1 To 0 Step -1 '// loop backwards thru ListView.
                .Items.Remove(.Items.Item(i)) '// remove item from ListView.
            Next
        End With
        '// looping backwards will not affect the "i" in the loop as looping forwards does when deleting items.

If you need to remove only a single item, then see if this helps.

With ListView1
            If .Items.Count = 0 Then Exit Sub
            If Not .SelectedItems.Count = 0 Then '// check if item is selected.
                .Items.Remove(.SelectedItems(0))
            Else
                MsgBox("Please select an item to remove from ListView.")
            End If
        End With

.Or this.

ListView1.Items.Remove(ListView1.Items.Item(2)) '// remove 3rd item from ListView.

thanks codeorder! I will try this next time to my application. For now I'm gonna close the thread :)

Public Class Form1
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim z As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\My Documents\ignou1.mdb"
cn.Open()
z = 1
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sql As String
Dim x As Integer = 0
sql = "select * from assign"
rs.Open(sql, cn)
Do While Not rs.EOF
ListView1.Items.Add(rs.Fields(0).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(1).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(2).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(3).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(4).Value)
rs.MoveNext()
x = x + 1
Loop
rs.Close()
End Sub

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
TextBox1.Text = e.Item.Text
TextBox2.Text = e.Item.SubItems(1).Text
TextBox3.Text = e.Item.SubItems(2).Text
TextBox4.Text = e.Item.SubItems(3).Text
TextBox5.Text = e.Item.SubItems(4).Text
z = z + 1
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

End Sub
End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.