Results 1 to 14 of 14

Thread: [RESOLVED] ListBox question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,172

    Resolved [RESOLVED] ListBox question

    Hi

    I am trying to arrange a listbox so that as each individual line is entered (happens to be a list of files harvested from a directory one by one) then the earliest item displayed pops off the top, so that the latest items.add is always showing.

    I did this with vb6 by default as far as I can see. How in 2010 please?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: ListBox question

    to always add the new item at the top of the listbox:

    vb.net Code:
    1. ListBox1.Items.Insert(0, "new item")

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,172

    Re: ListBox question

    Thanks, but I want to add items at the foot and *have the earlier items scroll off the top*

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: ListBox question

    Have a look at the ListBox's TopIndex Property.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: ListBox question

    this works:

    vb.net Code:
    1. ListBox1.Items.Add("new item")
    2. Dim maxItems As Integer = ListBox1.Height \ ListBox1.GetItemHeight(0)
    3. ListBox1.TopIndex = ListBox1.Items.Count - maxItems

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,172

    Re: ListBox question

    Hate to look a gift horse in the mouth, but isn't there a simpler way? What I am actually doing is showing in a list box the names of files I am copying from one drive to another, one by one as they get copied. I want to add each file to the foot and when the box is full, have the earliest files drop off the top, so that the latest file is always shown at the foot.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: ListBox question

    maybe someone else knows a way, but that's my solution

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,172

    Re: ListBox question

    OK. Do I have to put those lines inside the loop as the routine loops through all the files (there could be hundreds) or I can I set it once?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: ListBox question

    There may be an easier way, but I don't know of an automatic way, which is probably what you are looking for. I don't know of a property setting that will cause the listbox to automatically scroll to the bottom.
    My usual boring signature: Nothing

  10. #10
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: ListBox question

    Quote Originally Posted by .paul. View Post
    this works:

    vb.net Code:
    1. ListBox1.Items.Add("new item")
    2. Dim maxItems As Integer = ListBox1.Height \ ListBox1.GetItemHeight(0)
    3. ListBox1.TopIndex = ListBox1.Items.Count - maxItems
    You don't need to calculate the number of items that will fit in the ListBox.

    Code:
    Imports System.ComponentModel
    
    Public Class Form1
    
        Dim MyBindingList As BindingList(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MyBindingList = New BindingList(Of String) From {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
            ItemsListBox.DataSource = MyBindingList
        End Sub
    
        Private Sub InsertButton_Click(sender As Object, e As EventArgs) Handles InsertButton.Click
            MyBindingList.Add("New Item")
            ItemsListBox.TopIndex = ItemsListBox.Items.Count - 1
        End Sub
    End Class
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: ListBox question

    I got distracted and got in a bit late.

    The question you need to answer for yourself is: How do you want this to behave? After all, if you were to toss items into the listbox as fast as you obtained them, it is likely that the listbox will be pretty much a blur, as it will be scrolling by at a good clip most likely. That may not look good no matter what. Therefore, you might consider only adjusting the Listbox every N items, or so. Alternatively, you might want to only update on a timer, though that could get much more tricky. I think it comes down to the visual result that you want.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,172

    Re: ListBox question

    I am not worried about any such limitation. I simply want to list the files from the earliest to the last as they copy, and I want the earliest to drop off the top when the listbox becomes full.

  13. #13
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: ListBox question

    vb.net Code:
    1. ListBox1.Items.Add(filename)
    2.             ListBox1.SelectedIndex = ListBox1.Items.Count - 1

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,172

    Re: ListBox question

    Yep! That's it. Had just found it by Googling at length, and have been trying it. Just put that second line in after each item is added (ie in the loop). Thanks all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width