Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET Textbox input validation 4 62
I need help changing the default value for an @HTML.LabelFor control 4 34
Get sourcecode path 14 46
c#, case, if 4 16
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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

685 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