Solved

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

Posted on 2008-10-09
6
2,191 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now