Click here to Skip to main content
15,885,144 members
Articles / Web Development / ASP.NET

Horizontal Scroll Bar in a Combo Box or a Listbox with Up/Down Arrow Key Press Functionality

Rate me:
Please Sign up or sign in to vote.
4.81/5 (26 votes)
29 Jan 2006CPOL2 min read 285.1K   4.1K   43   22
This article describes getting a custom horizontal scroll bar in a combobox or a listbox with functionality as expected on pressing of up or down arrow keys.

Sample screenshot

Introduction

This article describes:

  • How to display a horizontal scroll bar in a select box (HTML) or listbox (ASP.NET)
  • On pressing up or down arrow keys, the functionality should also work as expected

The Problem

There are some limitations in Internet Explorer (IE) while rendering some HTML controls. One such control is the combobox (HTML) or the ListBox (ASP.NET).

Again, there are a couple of limitations, while rendering a combobox or a listbox in Internet Explorer:

  • In a combobox, if width is specified and the length of the content is greater than the specified width, then a horizontal scrollbar won’t appear.
  • Specifying the title attribute won’t display the tooltip.

I’ve gone through many articles from the net with respect to putting a horizontal scroll bar in a combobox or a listbox, but none looked good.

Finally, I got some articles which explained how to put a custom horizontal scrollbar for a combobox or a listbox. But again, there are some limitations in a custom scrollbar as up and down arrow keys won’t work as expected. We can achieve horizontal scroll bar in a combobox using a DIV tag by specifying the height and the width. Now, consider a scenario where the number of items in a combobox is greater than the specified height of the DIV element. Now, if we press down/up arrow keys, it won’t work as expected, i.e., we can’t see the selected item.

The Solution

There are a couple of things we need to work out to overcome the horizontal scrollbar issue in a combobox.

  • As we know, Internet Explorer doesn’t support horizontal scrollbars in a combobox, so we have to go with the custom horizontal scroll bar. From the look and feel and user point of view, horizontal scrollbar will appear as a part of the combobox.
  • Now we have added a DIV tag on top of the combobox. To overcome up/down arrow key issue, we need to apply some tricks, so that it should behave as expected.

Using the Code

HTML
<div id='divCollegeNames' 
  style="OVERFLOW: auto;WIDTH: 304px;HEIGHT: 147px" 
  onscroll="OnDivScroll();" >
HTML
<SELECT id='lstCollegeNames' size="8" 
  multiple onfocus="OnSelectFocus();" >

Couple of things we have to take care of at the script level:

JavaScript
//On scrolling of DIV tag.
function OnDivScroll()
{
    var lstCollegeNames = document.getElementById("lstCollegeNames");

    //The following two points achieve two things while scrolling
    //a) On horizontal scrolling: To avoid vertical
    //   scroll bar in select box when the size of 
    //   the selectbox is 8 and the count of items
    //   in selectbox is greater than 8.
    //b) On vertical scrolling: To view all the items in selectbox

    //Check if items in selectbox is greater than 8, 
    //if so then making the size of the selectbox to count of
    //items in selectbox,so that vertical scrollbar
    // won't appear in selectbox
    if (lstCollegeNames.options.length > 8)
    {
        lstCollegeNames.size=lstCollegeNames.options.length;
    }
    else
    {
        lstCollegeNames.size=8;
    }
}
JavaScript
//On focus of Selectbox
function OnSelectFocus()
{
    //On focus of Selectbox, making scroll position 
    //of DIV to very left, i.e., 0 if it is not. The reason behind
    //is, in this scenario we are fixing the size of Selectbox 
    //to 8 and if the size of items in Selectbox is greater than 8 
    //and to implement downarrow key and uparrow key 
    //functionality, the vertical scrollbar in selectbox will
    //be visible if the horizontal scrollbar of DIV is extremely right.
    if (document.getElementById("divCollegeNames").scrollLeft != 0)
    {
        document.getElementById("divCollegeNames").scrollLeft = 0;
    }

    var lstCollegeNames = document.getElementById('lstCollegeNames');
    //Checks if count of items in Selectbox is greater 
    //than 8, if yes then making the size of the selectbox to 8.
    //So that on pressing of downarrow key or uparrowkey, 
    //the selected item should also scroll up or down as expected.
    if( lstCollegeNames.options.length > 8)
    {
        lstCollegeNames.focus();
        lstCollegeNames.size=8;
    }
}

That's it!

Points of Interest

Impossible is just another hurdle on the track.

History

  • 29th January, 2006: Initial version of the article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
My name is Shiby Chacko. Currently I'm staying in Bangalore, the Silicon Valley of India. Working in product/service based software company. Working as senior software developer. Learning/experimenting new thing in software is my passion. Also I luv to enjoy life with my surrounding friends/people.

As an update, now I work with Microsoft as consultant in US

Comments and Discussions

 
GeneralScrollable select drop down list with tool tip Pin
NagendraMN14-Sep-09 20:49
NagendraMN14-Sep-09 20:49 
GeneralVertical scrollbar Pin
Nupur080620-Jan-09 5:24
Nupur080620-Jan-09 5:24 
GeneralAdvanced ListBox Component Pin
danludwig21-Jun-07 4:35
danludwig21-Jun-07 4:35 
GeneralYour Drop Down List/List Box Tool Pin
howardjr4-Oct-06 6:42
howardjr4-Oct-06 6:42 
NewsScrollableListBox Pin
Evyatar Ben-Shitrit15-Jun-06 8:59
Evyatar Ben-Shitrit15-Jun-06 8:59 
QuestionRe: ScrollableListBox Pin
smithasathyam12-Nov-06 20:01
smithasathyam12-Nov-06 20:01 
AnswerRe: ScrollableListBox Pin
smithasathyam12-Nov-06 20:04
smithasathyam12-Nov-06 20:04 
GeneralAnother Issue Pin
Tboner24-Feb-06 9:55
Tboner24-Feb-06 9:55 
QuestionRe: Another Issue Pin
jobaz5418-Jun-08 23:16
jobaz5418-Jun-08 23:16 
GeneralTweaked functions Pin
PMDL17-Feb-06 9:05
PMDL17-Feb-06 9:05 
AnswerRe: Tweaked functions Pin
Shiby20-Feb-06 21:41
Shiby20-Feb-06 21:41 
GeneralAlmost... Pin
Tboner14-Feb-06 5:32
Tboner14-Feb-06 5:32 
GeneralRe: Almost... Pin
PMDL17-Feb-06 9:12
PMDL17-Feb-06 9:12 
GeneralRe: Almost... Pin
PMDL17-Feb-06 9:41
PMDL17-Feb-06 9:41 
GeneralRe: Almost... [modified] Pin
zivros1-Jan-07 23:24
zivros1-Jan-07 23:24 
GeneralRe: Almost... Pin
danludwig11-Jun-07 6:14
danludwig11-Jun-07 6:14 
GeneralCool Pin
Voldo8012-Feb-06 15:20
Voldo8012-Feb-06 15:20 
GeneralWeb control Pin
dado200330-Jan-06 4:50
dado200330-Jan-06 4:50 
GeneralRe: Web control Pin
Shiby31-Jan-06 5:22
Shiby31-Jan-06 5:22 
Questionbad picture Pin
pesokes29-Jan-06 9:04
pesokes29-Jan-06 9:04 
AnswerRe: bad picture Pin
Shiby29-Jan-06 17:50
Shiby29-Jan-06 17:50 
GeneralRe: bad picture Pin
anil_mane31-Jan-06 1:08
anil_mane31-Jan-06 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.