Listview: Item always visible

Whenever you add new items to a listview control ListViewItems, and these are so numerous that the control does not have enough space to display them on screen, adding a vertical scroll bar. This is perfect, except that by default are not that new items are added.

Today I want to show how to solve this little problem.

Visual basic controls. The cone net listview give us an impressive array of properties for configuration, so that often are unknown.

I want to show a simple example. For that we need a form, a listview control and a button.

listviewItemSiempreVisible001

The properties that most interest us are:

  • HideSelection: Setting this to false if no listview control has the focus, we continue to see what the selected item.
  • ListView1.FocusedItem.EnsureVisible () to force the selected item to be always visible.

Well, now if the code:

     Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         ListView1.HideSelection = False
         ListView1.FullRowSelect = True
         ListView1.MultiSelect = False
     End Sub

     Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         Static i As Integer = 0
         i + = 1
         ListView1.Items.Add (New ListViewItem ("Item number" & i))
         ListView1.Items (ListView1.Items.Count - 1). Selected = True
         ListView1.Items (ListView1.Items.Count - 1). Focused = True

         ListView1.FocusedItem.EnsureVisible ()
     End Sub

Running it should look much like what is shown in this image:

listviewItemSiempreVisible002

That's all, if this information helped you, please leave a comment.

11 Responses to "Listview: Item always on top"

  1. Information Bitacoras.com ...

    Rate in Bitacoras.com: Whenever you add new items to a listview control ListViewItems, and these are so numerous that the control does not have enough space to display them on screen, adding a vertical scroll bar. This is .....

  2. MYSTIC said:

    Well we all know q to display the data but I q I'm interested if they can explain how are stored in a module are sawing the program aunq

  3. RCHARD says:

    A QUESTION I WANT THAT SAME BUT FOR EXAMPLE DataGridView IF NOT COULD HELP ME IN THIS ISSUE

    MY EMAIL:
    richardramos789@hotmail.com
    YOU WOULD BE GRATEFUL IF you help me

  4. Jorge says:

    Hello such,

    Look I have a doubt with a listview to implement on my system, I'm putting together a POS system and enter a code in my txtbox achievement in my listview print correctly, my question is that I want when registering the same product, rather than adding a new row is added to it and in the Quantity column will increase the number.

    For example if I add 12345, is printed in my listview, but if I add 12345 to place print on a new row is printed in a row but in column amount increases to 2, not if I explain or could guide me in this question that I have I would greatly appreciate.

    • Elisha says:

      I have no very clear what you want to do, but I suggest that you look at this example may be helpful: http://www.solovb.net/index.php/2009/02/19/modificar-datos- of-a-listview /
      Any further questions do not hesitate to contact me.

      Regards

    • Oscar says:

      Hello George, what you ask involves data persistence and to update a column belonging to the rank of a specific key in a table by 2 solutions you can choose: first you create a DB with the fields you need and use a connection either with MySQL using the adapter for it or the SQL Server is included with Visual Studio, the other way is through flat files that save the data in a file. txt and then proceed to the reading and rewriting of some data , for both options as you need to increase the value for a column rewquerida, you should also apply a function to increase this value based on the condition you require. :)

  5. Jorge says:

    Elisha thank you very much for your prompt response.

    Ok look I'll explain. I have a TextBox in which add the products I have in my DB, ok for example, Coca-cola, I enter the bar code on my TextBox in this case would be 12345678, good showing in my listview the product description with my columns listview are:

    Product Code, Description, Quantity, Sale Price, Existence.

    Then add the product to my row is full of such data in my listview, but if I re-insert the code of coca-cola, is shown in a next row, which I like to do is that when you insert another Once the product itself rather than in a new row, just change the number of my column Quantity, in this case exchange to 2.

    That's more than nd I want to do, if I could say more or less answer this question I'd appreciate it.

    Thanks

    • Elisha says:

      Well, now is a little lighter.

      The first thing you do before inserting a new line is to cross the listview to check if you have already entered the code you are trying to insert.
      That you do a For Each, for example

      "I think a variable to indicate whether I found the code already existed
      Dim exists As Boolean = False
      For each item as ListViewItem in listviewProductos.items
      Then if item.text = txtCodigo.text
      'If you come here is because it was already loaded
      cantidadAnterior dim as Integer = CType (item.subitm (2). text, Integer)
      item.subitem (2). cantidadAnterior + text = CType (txtCantidad.text, Integer)
      'If you find you notice that already existed
      exists = True
      end if
      Next

      If Not Then there
      'Here it is not found then insert
      End if

      Well, Espro serve ... you can not rule out a typo Terner since I wrote it directly here

      But surely you will serve

  6. Jorge says:

    Thanks again,

    See you commented that if I had worked on that issue by changing the value of the Quantity column, what happens is I'm extracting values ​​from a MySQL database I show looks like the code print my listview

    Listview Private Sub ()
    Dim i As Integer
    Dim s As Integer
    Dim count As Integer
    Subtotal Dim As Double
    Dim list As New ListViewItem

    As New MySqlCommand Dim string ("SELECT * FROM products WHERE ProductCode = '" & txtCodigoBarras.Text & "'", con)

    cadena.ExecuteReader mysqldr = ()

    While mysqldr.Read ()
    list = listView1.Items.Add (txtCodigoBarras.Text.Trim)
    listar.SubItems.Add (CStr (mysqldr ("Description")))
    listar.SubItems.Add (CStr (mysqldr ("Quantity")))
    listar.SubItems.Add (CStr (mysqldr ("PrecioVenta")))
    listar.SubItems.Add (CStr (mysqldr ("Stock")))
    End While

    Dim exists As Boolean = False

    For Each item As ListViewItem In ListView1.Items
    Then if item.Text = txtCodigoBarras.Text
    CantidadAnterior Dim As Integer = CType (listar.SubItems (2). Text, Integer)
    listar.SubItems (2). Text = cantidadAnterior + CType (mysqldr ("Quantity"), Integer)
    exists = True
    End If
    Next
    If Not Then there
    End If

    lblCantidadProd.Text = mysqldr.Item (3)
    lblPrecioUnidad.Text = mysqldr.Item (5)

    For s = 0.0 To ListView1.Items.Count - 1
    Subtotal = Subtotal + ListView1.Items (s). SubItems (3). Text
    Next
    lbLSubTotal.Text = "$" & Format (Subtotal, "0.00")

    For i = 0 To ListView1.Items.Count - 1
    counter = counter + ListView1.Items (i). SubItems (2). Text
    Next
    Me.lblContador.Text = counter

    mysqldr.Close ()
    End Sub

    The code that I spent so implement it and see the results in the following photo:

    http://img193.imageshack.us/img193/4421/productosv.jpg

  7. Jorge says:

    Friend haha ​​sorry if I upset with so many questions, but I'm finding more or less the problem, for example introduce the product

    DORITOSNAC

    Is inserted in the front row and when I put DORITOSNAC is printed on the second row but had already introduced the first changes its value from quantity to 2 and is printed back to enter again on a 3 row but equally the from 1 row changes its value to 3

    Then if txtCodigoBarras.Text = txtCodigoBarras.Text
    Dim exists As Boolean = False
    For Each item As ListViewItem In ListView1.Items
    Then if item.Text = txtCodigoBarras.Text
    CantidadAnterior Dim As Integer = CType (item.SubItems (2). Text, Integer)
    item.SubItems (2). Text = cantidadAnterior + CType (lblCantidadProd.Text, Integer)
    exists = True
    End If
    Next
    If Not Then there
    End If

    Let's see if I get it now if not any explanation on your part will be grateful.

  8. Jorge says:

    already achieved solve many thanks for your support get the solution:

    Dim count As Integer
    For j As Integer = 0 To Me.ListView1.Items.Count - 1
    If Me.ListView1.Items (j). SubItems (0). Then Text = txtCodigoBarras.Text
    count = CInt (Me.ListView1.Items (j). SubItems (2). Text) + 1
    Me.ListView1.Items (j). SubItems (2). Text = count
    Exit Sub

Comment