Solved

Selecting last item in list in listbox control automatically - C# 2005

Posted on 2007-11-25
8
4,348 Views
Last Modified: 2013-12-16
Hello,

Does anyone know how to select the last item in a ListBox control on a windows form in using C#??

I have a form with a listbox bound to a SQL table. The listbox displays data from one of thr columns in the table. The user is able to add new records and the new record column shows up in the list box. The new record shows up as the column data at the bottom of the list box.

1. Is there a way to have the new record's data show at the top of the list box?
2. Is there a way to have the largest value in the list box maintain the cursor's focus?

Thanks!
0
Comment
Question by:Saxitalis
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 400 total points
ID: 20347271
Hi Saxitalis,

See the below code snippet.

Regards,

Wayne
      //to select the last item in the listbox

     ListBox1.SelectedIndex = ListBox1.Items.Count - 1; 
 
 

      //to select the highest value in the listbox

      //select the first item to begin with

     for (int i = 0; i <= ListBox1.Items.Count - 1; i++) {

         if (ListBox1.Items(i) > ListBox1.SelectedItem) {

             ListBox1.SelectedIndex = i;

         }

      }

Open in new window

0
 
LVL 4

Expert Comment

by:Smallfish2004
ID: 20348521
I would add: if you want the new item to be first you can use the Insert method instead of Add
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 20348668
listbox1.Items.Insert(0, <<object>>);
0
 

Author Comment

by:Saxitalis
ID: 20350436
Hmm - sorry if I wasn't clear...

My list box gets it's data from a SQL table. New records are addded with an Insert statement in a stored procedure. The sproc is called from my C# managed code. When I click on  the button to run the SQL SERVER Sproc, a new record gets inserted into the table and a new value shows in the listbox.

 The problem is: The newest item added to  list box (from the SQL table update) gets shown at the bottom of the Listbox's list. Then you have to scroll down through the entire ListBox list to see it. (note. the inserted number will allways be the largest in list)

Being able to have the newest item in the list highlighted (same as if you manually clicked on it) would solve my problem.

Is there a way to highlight the newest (will always be the largest) item in the ListBox list from my code??? So that the item in the list will be highlited on the windows form??

Thanks!

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20352761
Have you tried either of my suggestions? The first one selects the last item in the listbox, and the second one selects the highest.
0
 

Author Comment

by:Saxitalis
ID: 20353546

 Wayne - Sorry for not listening :)

 I am unsure how to use this code. I guess that I would need to somehow find the index of the last item using your code example " ListBox1.SelectedIndex = ListBox1.Items.Count - 1; " and then relate that index to the acual value in the array? Then set that acual item value to the listbox text porperty??

Is this the right idea?

Thanks!



Could you
0
 

Author Comment

by:Saxitalis
ID: 20355260

 I actually just solved my problem by sorting the underlying SQL view differently.  

Thanks again for helping me think it through!!

John
0
 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 100 total points
ID: 20355719
int newindex = this.listbox1.items.count -1;
this.listbox1.SetSelected(<<newindex>>, true);

this might help you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now