Solved

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

Posted on 2007-11-25
8
4,361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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
 
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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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