Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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 keystrokes in Netbeans IDE 8.0 for Windows.

863 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

22 Experts available now in Live!

Get 1:1 Help Now