Solved

ListBox, selectionMode: multiple , selected item does not scroll to the top

Posted on 2008-10-09
6
2,209 Views
Last Modified: 2013-12-17
I have listbox , and its selectionmode is multiple. when I select an item programmatically, the item gets selected(gets highlighted in the listbox), however the selected item does not get scroll to the top. I am using web C#.
0
Comment
Question by:lms036000
[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
  • 2
  • 2
6 Comments
 

Expert Comment

by:alexmac05
ID: 22683040
I believe that you will have to write an algorithm to add this functionality.

I wrote this to show how to just swap the two values but you would need something that would change them such that all of the values would switch.

int intIndex = listBox1.SelectedIndex;

if (intIndex != -1) //when something is unselected it is -1
{
   string strSelectedItem = listBox1.Items[intIndex].ToString();
   string strZeroItem = listBox1.Items[0].ToString();

   listBox1.Items[0] = strSelectedItem;
   listBox1.Items[intIndex] = strZeroItem;
}



0
 

Expert Comment

by:alexmac05
ID: 22683047
The fact that you have multiselect turned on does also complicate it.

I do not know of another way to do this other than to write the code yourself.
0
 
LVL 1

Accepted Solution

by:
zonkerman earned 500 total points
ID: 22697315
This code I put together may do the trick.  First it populates a list box to 100 entries. Then it calls a routine I wrote which selects item 92 and scrolls it to the top of the list if it can be done.  

 
      private void button3_Click(object sender, EventArgs e)
        {
            PopulateListBox();
            SelectItemAndScrollToTop(listBox1, 92);
        }
        private void PopulateListBox()
        {
            for (int i = 0; i < 100; i++)
            {
                listBox1.Items.Add("entry " + System.Convert.ToString(i));
            }

        }

        private void SelectItemAndScrollToTop(System.Windows.Forms.ListBox aListBox, int SelectedIndex)
        {
            int LinesDisplayed = aListBox.ClientSize.Height / aListBox.ItemHeight;
            listBox1.SetSelected(92, true);
            if (
                (aListBox.Items.Count > LinesDisplayed) &&
                (
                  (aListBox.Items.Count - aListBox.SelectedIndex) > (LinesDisplayed * 2) ||
                  (  
                    (aListBox.SelectedIndex > LinesDisplayed) &&
                    ((aListBox.Items.Count - aListBox.SelectedIndex) < (aListBox.Items.Count - ((LinesDisplayed * 2))))
                  )
                )
               )
            {
                aListBox.TopIndex = aListBox.SelectedIndex;
            }
        }
0
 
LVL 1

Expert Comment

by:zonkerman
ID: 22697342
okay one minor problem with that sample I provided.  I used 92 as the sample item number to select but it will only scroll to the top if the listbox on your form is sized to display 8 items at once.  I therefore forgot to say make sure for the sample to make a listbox that displays only 8 items.  The routine SelectItemAndScrollToTop should work regardless of the size of the listbox provided it is possible to scroll the selected item to the top.

0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

729 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