Listbox: scroll to the selected item by code

时间:2023-01-19 10:03:43

I have code that selects - find and select - 1 item in a large listbox. This
works Ok but most of the time the selected item isn't visible without
scrolling down manualy. Is this possible by code?

------------------

I'm marking listbox items as selected, and I'd like to scroll listbox to
a selected entry if it is off-screen. How can I do it?

-------------------

You can use JavaScript to reset the scroll position though. When the page is submitted to the server store the scroll position in a HiddenField (make sure it's an ASP.NET HiddenField so that the ViewState can be remembered). That way when the request comes back you can use JavaScript to retrieve the scroll position and reset the scroll position.

----------------

(C#, Windows Form)
this.listView1.Select();
this.listView1.Items[ret].Selected = true;
this.listView1.EnsureVisible(ret);

----------------

ASP.NET, C#, Ajax (NOTE: server script does not work; client script work correctly.)

ScriptManager.RegisterStartupScript(this, this.GetType(), "EnsureSeletedItemVisible", "document.getElementById('"+this.lbUnassigned.ClientID+"').selectedIndex="+this.lbUnassigned.SelectedIndex.ToString(), true);

 

REF:

http://bytes.com/topic/c-sharp/answers/257674-listbox-scroll

http://bytes.com/topic/access/answers/191145-listbox-scroll-selected-item-code