Solved

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

Posted on 2007-11-25
8
4,357 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 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