Click here to Skip to main content
6,822,123 members and growing! (18,704 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

Horizontal scroll bar in a combo box or a listbox with up/down arrow key press functionality

By Shiby

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.
C#, VB, Javascript, .NET, Win2K, WinXP, Win2003, Vista, ASP, ASP.NET, WebForms, VS.NET2003, VS2005, IE6.0, Dev
Posted:29 Jan 2006
Views:147,384
Bookmarked:36 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 6.35 Rating: 4.67 out of 5
1 vote, 4.3%
1

2
1 vote, 4.3%
3
3 votes, 13.0%
4
18 votes, 78.3%
5

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 of such controls is the combobox (HTML) or the ListBox (ASP.NET).

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

  • In a combo box, 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 tool tip.

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, IE 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

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

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

//On scrolling of DIV tag.

function OnDivScroll()
{
    var lstCollegeNames = document.getElementById("lstCollegeNames");

    //The following two points achieves 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 vertival scrollbar

    // won't appear in selectbox

    if (lstCollegeNames.options.length > 8)
    {
        lstCollegeNames.size=lstCollegeNames.options.length;
    }
    else
    {
        lstCollegeNames.size=8;
    }
}
//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 Selecbox 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 exremely 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

Initial version of the article - 29 Jan 2006.

License

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

About the Author

Shiby


Member
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
Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralScrollable select drop down list with tool tip PinmemberNagendraMN21:49 14 Sep '09  
GeneralVertical scrollbar PinmemberNupur08066:24 20 Jan '09  
GeneralAdvanced ListBox Component Pinmemberdanludwig5:35 21 Jun '07  
GeneralYour Drop Down List/List Box Tool Pinmemberhowardjr7:42 4 Oct '06  
NewsScrollableListBox PinmemberEvyatar Ben-Shitrit9:59 15 Jun '06  
QuestionRe: ScrollableListBox Pinmembersmithasathyam21:01 12 Nov '06  
AnswerRe: ScrollableListBox Pinmembersmithasathyam21:04 12 Nov '06  
GeneralAnother Issue PinmemberTboner10:55 24 Feb '06  
QuestionRe: Another Issue Pinmemberjobaz540:16 19 Jun '08  
GeneralTweaked functions PinmemberPMDL10:05 17 Feb '06  
AnswerRe: Tweaked functions PinmemberShiby22:41 20 Feb '06  
GeneralAlmost... PinmemberTboner6:32 14 Feb '06  
GeneralRe: Almost... PinmemberPMDL10:12 17 Feb '06  
GeneralRe: Almost... PinmemberPMDL10:41 17 Feb '06  
GeneralRe: Almost... [modified] Pinmemberzivros0:24 2 Jan '07  
GeneralRe: Almost... Pinmemberdanludwig7:14 11 Jun '07  
GeneralCool PinmemberVoldo8016:20 12 Feb '06  
GeneralWeb control Pinmemberdado20035:50 30 Jan '06  
GeneralRe: Web control PinmemberShiby6:22 31 Jan '06  
Questionbad picture Pinmemberpesokes10:04 29 Jan '06  
AnswerRe: bad picture PinmemberShiby18:50 29 Jan '06  
GeneralRe: bad picture Pinmemberanil_mane2:08 31 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 29 Jan 2006
Editor: Smitha Vijayan
Copyright 2006 by Shiby
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project