Solved

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

Posted on 2007-11-25
8
4,358 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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