?
Solved

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

Posted on 2007-11-25
8
Medium Priority
?
4,366 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 1200 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 300 total points
ID: 20355719
int newindex = this.listbox1.items.count -1;
this.listbox1.SetSelected(<<newindex>>, true);

this might help you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

743 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