Hi guys,
I have a form called frmSales, with two textboxes and a list box.
what I want to do is to fill the listview with product description from txtDescription and product from txtPrice when I click the add to cart button. the listview should also be able to add more items when the add button is clicked again, this time the items that are added after the should be added below the already added items.

I tried to run a simple code. It could fill the listview with the items, but when I try to add more, they are not added below the other items, but rather to the right of the other items.
the code I tried to use is;

 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
      Dim List1 As ListViewItem

        List1 = Me.lsv1.Items.Add(Me.txtDescription.Text)
        List1 = Me.lsv1.Items.Add(Me.txtPrice.Text)
End Sub

please, help me display the data in the right order.

Recommended Answers

All 12 Replies

You need to add

 &VbNewLine

at the end of the add statement so that when you click twice it will write the data on a new line below the previously added data.

An example based on your above code will be:

 List1 = Me.lsv1.Items.Add(Me.txtDescription.Text & Vb.NewLine)
 List1 = Me.lsv1.Items.Add(Me.txtPrice.Text & Vb.NewLine)

Something like this should help do what you intend to do.

Hi Mr.M. Thanks for your response.
I tried to add the lines you suggested, it still did not insert a new selection as a new line below the existing entry in the listview.
Anymore ideas?

I've just worked with another project where the OP was also doing something similar except that, that's OP wanted to make more then one selections at the same time, as I worked with that's OP's question I noticed that even if you add the code for new line, the new line won't be added so I prefer you use ListBox instead of ListView, unless its a must do with a ListView.

ListView seemed to me that it had some limitations on doing things with it. With a ListBox what you require can be achieved so early.

Hi

It sounds like you have the View property of your ListView set to LargeIcons or one of the icon views. Change this property to List and each item will be placed below when the button is pressed.

Also, you do not need to use the ListViewItem to add items in the way that you are, you can simply add them directly to the ListView control.

HTH

@DJ but there are situations where the items are retrieved somewhere maybe from a file or from a database then are added to a control, so I think we shouldn't stick much to the way of how the OP is adding because we may find that the data is retrieved some where else and it is dynamically updated so the OP may need to add it just like how he do, unless the data won't be updated at a later stage then the OP can add it internally or within IDE. Just saying

@Mr.M, I totally agree that data can come from anywhere and that depending on the situation or how the data should be displayed will determine on the choice of control type.

However, I would not agree with adding new line characters to the data in order to get it to display vertically. If it is a ListBox, then simply add each piece of data to the Items collection. This saves having to strip any data back out should it interfere with how that data will be used later on.

In this particular case, however, the OP is stating that data is being added to the right of existing data within a ListView control which immediately tells me that he/she has the View property set to something like LargeIcons or SmallIcons. This is easily tested. Furthermore, if I were creating some form of shopping cart where I wanted to display an item and price then a ListView or DataGridView would be good candidates for this and I personally would use two columns, one for description and one for price to make the display even nicer rather than having price below the description. Furthermore, both the ListView and DataGridView allow in place editing so the user experience is even better than that offered by a ListBox.

Hi djjeavons,
I got your point. I think I was not explaning things the correct way.
The two textboxes on the form should fill the listview with - description in the first column and price in the other column. And when I add another item, it should be added in the same manner on a new line.

I hope this will make you understand what I mean.

Your usual help will be appreciated.

@Dj yes I agree, I posted the use new line early then later when I was in front of my pc I then noticed that its not working. But I got your point there.

@Wishala you should have cleared that out, if so then you can use Dj's suggestion.

Have a look at this post which details how to add two columns to a list view and populate. You will only need to make a small modification to incude the data from your text boxes.

You also need to add like this:
ListView1.Items.Add(Me.txtDescription.Text).SubItems.Add(Me.txtPrice.Text)

Thank you so much guys. A collection of your replies has made my project work.

Thank you once again.

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.