VB: Howto Auto-select Last Item In Listbox

free web hosting
Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

VB: Howto Auto-select Last Item In Listbox

Philywiskaz
Hi,

I'm just writing a small program with a friend that is kind of pointless at the moment smile.gif but there was one issue that we haven't been able to work out with a listbox and we aren't too sure if its even possible to do it. unsure.gif

My question is, is it possible to make a listbox automatically scroll to the bottom of the list when a new item is added? if so can anyone please let me know?

Thank you all in advance for your help.

P.S. Sorry if it's a silly question that i should know the answer to, but neither of us have touched VB in ages.

Reply

miCRoSCoPiC^eaRthLinG
Hi,
    This can very easily be achieved if you're using VB.NET. Here's an example:

1. I have a list box named lstBox. It's got a couple of items added to it beforehand. I add a new item:
lstBox.Items.Add ( "NewItem" )

2. Now the number of items in a listbox is returned if you call the Count method of ListBox.Items. So:
lstBox.Items.Count - will give me the total item count including the newly inserted item.

3. Now third step is to select the last item - which can be achieved by using the SelectedIndex method of Listbox. SelectedIndex accepts an integer as it's parameter to indicate which item to select. Keep in mind, that this index is ZERO based. So the indices of the items in the list, start from 0 ..upto.. (n-1), where n = lstBox.Items.Count

So all I need to do is, lstBox.SelectedIndex = lstBox.Items.Count - 1 - this single line will do the trick smile.gif

Hope this helps...
Regards,
m^e

 

 

 


Reply

Philywiskaz
smile.gif Thank you very much M^E,

We are using VB6, but the code that you gave helped me work out the VB6 alternative and it is now working.

"lstBox.ListIndex = lstBox.ListCount - 1" as you can see very little has changed smile.gif

Reply

miCRoSCoPiC^eaRthLinG
Glad that helped smile.gif Yup - a lot of stuff can still be done with VB6 with minor modifications of .NET syntax. After all .NET is a superset of the old visual studio - so many methods are based upon the old ones - simply with newer names and more added techniques for easier use.

Issue resolved. Topic closed. smile.gif

Reply

FeedBacker
Im trying to figure out how to select all items in a listbox so I can move them from 1 listbox to another on click.I figured out how to make a timer to do this but how would I stop the timer after it has moved every item 1 time or is there a easier way ? Im working in vb6



-newbietoVB6

Reply

FeedBacker
how to auto move between contents in a list box using a timer
VB: Howto Auto-select Last Item In Listbox

I'm also writing a simple program at the moment. I want to auto scroll the selected items in a listbox using a timer so when the item is selected it displays on a textbox and I can use two control buttons to stop and start. By this way I could choose a random number from the listbox.
I just need some help for the code to move between items in a listbox automatically using timer control.

Thanx in advance

-question by aisha

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Recent Queries:-
  1. listview vb6 auto select - 1.09 hr back. (1)
  2. vb6 listbox selecteditem - 1.36 hr back. (1)
  3. how to select all items in list box using visual basic - 1.44 hr back. (1)
  4. selecting mulitple item from listbox but getting one item is selected - 1.85 hr back. (1)
  5. remove last item from listbox in vb - 2.78 hr back. (1)
  6. vb6 listbox selected - 2.98 hr back. (1)
  7. vb listbox select item - 3.19 hr back. (1)
  8. using items from one listbox to another in vb2005 - 3.83 hr back. (1)
  9. vb listview last items - 5.33 hr back. (1)
  10. autoselect listboxitem in asp.net - 8.51 hr back. (1)
  11. c# listbox scroll to bottom - 8.75 hr back. (1)
  12. how to select items in listbox using index - 11.73 hr back. (2)
  13. vb.net listbox select item count - 14.38 hr back. (1)
  14. vb selecting item in listbox - 18.61 hr back. (1)
Similar Topics

Keywords : vb, howto, auto, select, item, listbox

  1. Auto-number Help In Access Db & Vb .net
    (6)
  2. VB.NET: Howto Add And Delete Files
    Just looking for useful code (9)
    I am horrible at dealing with files in VB. I was wondering if anyone could give me some general
    code for a program I'm working on. I need to be able to add to files, display them, and delete
    them. I have use code out of a book, but it never works very well. Any suggestions?....

    1. Looking for vb, howto, auto, select, item, listbox






*SIMILAR VIDEOS*
Searching Video's for vb, howto, auto, select, item, listbox
advertisement




VB: Howto Auto-select Last Item In Listbox