|
-
Aug 10th, 2012, 02:49 PM
#1
Thread Starter
Frenzied Member
[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?
-
Aug 10th, 2012, 03:30 PM
#2
Re: ListBox question
to always add the new item at the top of the listbox:
vb.net Code:
ListBox1.Items.Insert(0, "new item")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2012, 04:00 PM
#3
Thread Starter
Frenzied Member
Re: ListBox question
Thanks, but I want to add items at the foot and *have the earlier items scroll off the top*
-
Aug 10th, 2012, 04:17 PM
#4
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.
-
Aug 10th, 2012, 04:19 PM
#5
Re: ListBox question
this works:
vb.net Code:
ListBox1.Items.Add("new item")
Dim maxItems As Integer = ListBox1.Height \ ListBox1.GetItemHeight(0)
ListBox1.TopIndex = ListBox1.Items.Count - maxItems
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2012, 04:28 PM
#6
Thread Starter
Frenzied Member
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.
-
Aug 10th, 2012, 04:30 PM
#7
Re: ListBox question
maybe someone else knows a way, but that's my solution
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2012, 04:32 PM
#8
Thread Starter
Frenzied Member
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?
-
Aug 10th, 2012, 04:33 PM
#9
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
 
-
Aug 10th, 2012, 04:34 PM
#10
Re: ListBox question
 Originally Posted by .paul.
this works:
vb.net Code:
ListBox1.Items.Add("new item") Dim maxItems As Integer = ListBox1.Height \ ListBox1.GetItemHeight(0) 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.
-
Aug 10th, 2012, 04:41 PM
#11
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
 
-
Aug 10th, 2012, 04:43 PM
#12
Thread Starter
Frenzied Member
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.
-
Aug 10th, 2012, 04:58 PM
#13
Re: ListBox question
vb.net Code:
ListBox1.Items.Add(filename)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
-
Aug 10th, 2012, 05:07 PM
#14
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|