Listview ... "A friend inseparable"

In each development is almost inevitable that I do have to use a listview. So I consider it very useful control. At first it cost a bit to understand its operation and it took several months to realize their full potential ... but since we understand we are inseparable.

Definition
To define a simple tell a listview is a collection of ListViewItem's

In order to populate a listview must first create a ListViewItem object (or more) and then appends the object to listview using the add method of the items property.


 'Is supposed to have a form called Form1 WinForm
 'With a listview control called ListView1

 Dim item As ListViewItem
 item = New ListViewItem ("Customer 1")
 ListView1.Items.Add (item)

 'So we can fill using a for loop / next
 For i As Integer = 1 To 10
 Dim item As ListViewItem
 item = New ListViewItem ("Customer" & i)
 ListView1.Items.Add (item)
 Next

A ListViewItem can store various data such as NAME, LAST,
ADDRESS, etc..

This requires adding (add) to each ListViewItem the necessary subitems.


 Dim item As ListViewItem
 item = New ListViewItem ("Name 1")
 'Now this I add an item object
 'Sub-item for each additional data
 item.SubItems.Add ("Name 1")
 item.SubItems.Add ("Address 1")
 item.SubItems.Add ("Phone 1")

 'It only remains to add the item to order
 'ListViewItem's collection of objects of
 'ListView1 the same way we did before
 ListView1.Items.Add (item)

A listview has different views to display data, these are Details,
LergeIcon, List, SmallIcon and Tile.

Following is a description of each one taken from the help of visual studio:

Details Each item appears on a separate line with more detailed information
of each element organized into columns. The column on the far left contains
a small icon and a label, and subsequent columns contain subitems
specified by the application. The columns include a header that can display
a title for the column. The user can change the size of each
column at runtime.
LargeIcon Each item appears as an icon size below which contains a
label.
List Each item appears as a small icon to the right of which contains a label.
The elements are arranged in columns without header.
SmallIcon Each item appears as a small icon to the right of which contains a label.
Tile Each item appears as an icon labeled size of element e
subelement information located on your right. The application specifies the information
subelement of that appears. This view is only available in Windows XP and
the family of Windows Server 2003. In earlier operating systems, this value
is ignored and the ListView control is shown in the view
LargeIcon

.

To view the data when we use the view "Details" we must first create
listview columns agree to the amount of items and subitems we have.


 Dim column1 As New System.Windows.Forms.ColumnHeader
 Dim column2 As New System.Windows.Forms.ColumnHeader

 Dim column3 As New System.Windows.Forms.ColumnHeader
 Dim As New System.Windows.Forms.ColumnHeader column4

 column1.Text = "Name"

 column2.Text = "Last Name"
 column3.Text = "Address"
 column4.Text = "Phone"

 column1.Width = 40
 column2.Width = 30
 column3.Width = 30
 column4.Width = 20

 'It is always important to make sure there are no
 'Columns and it is best to delete them before
 'Add the new columns

 ListView1.Columns.Clear ()

 'Now add the new columns
 ListView1.Columns.Add (column1)
 ListView1.Columns.Add (column2)
 ListView1.Columns.Add (column3)
 ListView1.Columns.Add (column4)

 ListView1.View = View.Details

The column settings can also be done in design mode from the
columns property, it s not open a wizard from which you administer
listview columns.
Properties

There are some very useful properties that can not fail to know then
mention that I use most. The remaining properties are left to
capaciddad exercise your research.

FullRowSelect SETTING This property always a True and its function is to highlight the whole line when
Working in Details view.
MultiSelect Enables (true) or disable (false) the selection of multiple elements within
listview.
HideSelection If the property is True you will not see that item is selected when the control
loses focus. In some cases it is important to disable (false) this option
to keep track of the active item.

29 Responses to "Listview ..." An inseparable friend ""

  1. quispe child jesus rodriguez says:

    Good day doctor:
    first hope not bother, I q help me regarding how to fill the listview, always use the DataGridView .. well the truth and not fill my listview c as they've tried q is not the same ... of course I want to fill my listview with the record returned from a sqlserver table using a query by example "SELECT ID, Description FROM TablaPaises"

    would like to know also how to add a record in a textbox q I have my listview but without affecting my database, and when you click on the button to record from my window I'm working the register q q add to my listview now be added if my database ...

    I hope can help .. and thanking you for your help .. and its contribution ...

    attn. jesus rodriguez child quispe

  2. quispe child jesus rodriguez says:

    Thanks for your response Dr. .. if I read the article, and helped me a lot .. I owe the time .. as always .. being helped with problems like these and to help with what we know .. makes growing knowledge and more to find new things .. design and other more ...

    thanks doctor, problem solved

  3. Jimmy Saenz says:

    such, first of all, congratulations on your blog, I have done much. I would like you to answer a concern I have if not too much trouble, since you have a great command in the listview control, I'm doing a vbnet player. I use the listview for all panels (playback, playlists, filters ...), in the case of search, a position in a listview all the songs that were added to the database, the form is a textbox where you enter the song to find and press the search button, I use a FindItemWithText to search .. and all good, in a msgbox, print the song is or not, perooo the favor I need from you is that I want to do a focus in the listview item found not like to add index items, simply use: listview.items.add (CancionesEncontradas)
    then, if you can help please be waiting for a prompt respuesta.gracias

  4. Jimmy Saenz says:

    buenisimo the post, simple and interesting, and solve the little problem thanks. you comment on the previous post I'm doing the small project of a player, I wish we were in contact for a small query, if you can add me to your contacts would be great, a? but, apart from WMP control between VB net there is another way of playing mp3, to not depend on the wmp library? waiting for your quick response again, a thanks in advance

  5. quispe child jesus rodriguez says:

    Good day doctor.

    again using their help, I would like to know the difference between datagridview and listview.
    and controls which of the two highly advised me to use for management of more than 1000 records per table in a database ..

    I owe beforehand.

    feliciandole and again for the great contributions ...

  6. Elisha says:

    I recommend reading this post that I believe will be of great help:

    http://social.msdn.microsoft.com/Forums/es-ES/dotnetes/thread/faea77af-edec-41a9-970f-cedb07542e73

  7. deVIShop says:

    saludoss:
    I read A LOT THE listview handle q

    I have a dudaa qe had for almost three days and could not solve q is

    How do you calculate the date range you are in a column in a listview?

    ---------------

    Description:
    schedule a DateTimePicker for each time q q Choose a date will be added in a column of list view ..

    The question is q:
    Once added to the listview not find a way to get the first date interval with the second, the second with the third i so on ...

    talves you present a topic like how to calculate q dates were added in a listview ...
    IF you have any recommendation or response will wait your consego xD

    something a little more descriptive of the problem I have in this web q :::

    http://www.forosdelweb.com/f79/sumar-fechas-listview-711801/

    GRACIASSSSSSSSSSSS .....

  8. Angelandro Great says:

    a question about your post,
    how to add 2 items to my listview with the property equal to largeicons view and I would love for each items had a different image?

  9. Lopera Nicko says:

    Regards,

    agredezco your cooperation in advance. My question is the following: if I have already agredados several items to a listview. none of these is selected (so I have not been clicking on it).
    how do I know that the code listview has no item selected?

    listview.listitems something like (i). select in vb 6.0

    many thanks

  10. Sauro cat says:

    as we have a friend in common : D just in vb.net 2008 can not find all the options (properties) that were useful to set me ... will have the same capacity as that of vb 6??

    • Elisha says:

      Hi

      I commented that the new ListView Control visual basic. Net exposes most of the properties and methods which had in Visual Basic 6 and only need to consider some names have changed.
      The ListView control exposes only some new properties. This group includes Activation (specify if the elements are activated with one or two clicks), HeaderStyle (if you can click on the titles of the columns) Scrollable (to be defined as False to prevent movement), LargeImageList (ImageList control used images for large icons mode) and smallImageList (ImageList control used in all other modes). Note that the property view defines the display mode, as in Visual Basic 6, but the Report has been renamed so as far as Details.

      I hope I have cleared some of your doubts.

      Regards

  11. Gabriel says:

    Hello I hope you can answer ...
    I scheduled a listview to carge me a list of customers directly from the database, my problem is I want you to select the customer with the option FullRowSelect listview and return to get the data from the selected row, so back to create an object "customer" data from the listview, so idk if will understand as I explain, thank you very much for everything, thanks for your time.

  12. Scale says:

    Hi Elisha, I first want to congratulate you on the blog .. is very good and served me well.
    Now I make a query:
    Considering the first example
    item = New ListViewItem ("Name 1")
    'Now this I add an item object
    'Sub-item for each additional data
    item.SubItems.Add ("Name 1")
    item.SubItems.Add ("Address 1")
    item.SubItems.Add ("Phone 1")
    'It only remains to add the item to order
    'ListViewItem's collection of objects of
    'ListView1 the same way we did before
    ListView1.Items.Add (item)

    As I can do to pass the subitems at different textbox??

    Greetings and thanks

    • Scale says:

      And solve it, right out of the guide to change the listview.

      Regards

    • Elisha says:

      to refer to the different subitems just use an index as I show below:

      TextBox1.Text = item.subitems (0). text
      textBox2.Text = item.subitems (1). text
      textBox3.Text = item.subitems (2). text

      Luck

  13. martin says:

    I need help with a question that I have long ago and no one could solve it. Position a full database in a listview, the issue is that as I'm adding these data are loaded at the end and there comes a time not look like I can do to keep the scroll bar at the end to see the last thing I adding.
    From already thank you very much

  14. Da Silva said in July:

    Friend I have a problem. Restarting my operating system or reformat my computer on my system I can not see the listview from the programming code. I'm using Visual Net 2003.
    To give a little more information the program was first created in VB4 to VB6 and now migrated to work in Visual Net 2003. I need to install to see the source code in listview .. -

  15. kiraleo says:

    olahe much sought on listview and I would like to help me with the move to a next item in a listview
    as a function paar listview.nextitem by code as one of two buttons to do next and a previous

    appreciate your help

  16. Padarinis says:

    Hello in There was VB.60 the possibility of putting a key and it was hidden. What happens is that I have to fill a listview with numbered from 1 to 10, but the ID of each customer has to be hidden. so when you open a listview. Puero ina window to load the data from which would be hidden dela ID. AH. and also it could place icons in each row ..

  17. gabox says:

    Hi

    Very good your blog also has a lot of aesthetics ... that is rare in developers ;)
    Now, my question is this:
    Any example that fill a listview from a hashtable that contains multiple instances of a specific class? I know I use a Foreach, but when running, the listview is "blank". I do not know if I express it.
    Thank you very much for your reply.

    • gabox says:

      Hi

      About my problem I clarified that I discovered how to make it work, just that I do outside the tabcontrol which was the listview, because I was in a tabcontrol and what will not work?
      Thank you very much for any response.

  18. Miguel says:

    hello
    I hope someone can help me.
    my problem with list view is that I send information via code because I generate the keys and data of my products with a bit of information that gives me the user and the data after the ones in the listview send them to a base data, until I have everything here but usually well before saving the user needs to modify data in the subitems are and what I can do is to modify the sub-items DATA someone can help me because I panic and can not find the solution at hand thanks

  19. John says:

    Hello,

    A query, as I can tell when activated horizontal scrool a listview in vb net.

    Regards

    Juan carlos

  20. Diana says:

    Hello,

    I would like to know how to post pictures in a listview, ie placed in columns fetched data from a database using dataset but I want that when aparezce the data such as name appears next to the image of that person.

    I will appreciate much your help

  21. Alfredo says:

    I would like to know how to post a Listview with an Access table, an example sencillito because there is no way to get it.
    Thanks

Comment