Solved

How do I add horisontal scrollbars in c#

Posted on 2006-06-27
5
363 Views
Last Modified: 2008-01-09
Hi.

I´m having problems adding horisontal scrollbar in my 'asp:listbox'.

And by the way, my vertical scrollbar doesn´t show up, I thought
they showed automaticly.
0
Comment
Question by:jenniferw
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:five22bags
ID: 16992052
The vertical one should show up if you set TextMode="MultiLine" within your <asp:TextBox>.

I have never seen or heard of an asp:textbox with a horizontal scrollbar.
0
 

Author Comment

by:jenniferw
ID: 16992103
Well you see, my listbox width is smaller than the filenames in showed the listbox, and I
want the user to be able reading this filenames, thats why the horisontal scrollbar is so
interesting. I don´t want to change the size of the listbox width, and thereby destroy my
layout...
0
 
LVL 11

Accepted Solution

by:
five22bags earned 500 total points
ID: 16992966
Wow, I sincerely apologize for not reading more carefully.... LISTBOX not TEXTBOX.

Horizontal scrollbars are actually a limitation of HTML, rather than ASP.NET. To trick the HTML, you will have to put your listbox inside of a DIV element. You can copy and paste the following code to see what I mean:

<div id='hello' style="Z-INDEX: 102; LEFT: 13px; OVERFLOW: auto; WIDTH: 247px; POSITION: absolute; TOP: 62px; HEIGHT: 134px" >
                        <asp:ListBox id="ListBox1" runat="server" Width="400px" Height="134px" BorderStyle="None">
                              <asp:ListItem Value="This is a very very very very very very very very long product">This is a very very very very very very very very long product</asp:ListItem>
                              <asp:ListItem Value="Computer">Computer</asp:ListItem>
                              <asp:ListItem Value="Monitor">Monitor</asp:ListItem>
                              <asp:ListItem Value="Keyboard">Keyboard</asp:ListItem>
                              <asp:ListItem Value="oops">oops</asp:ListItem>
                              <asp:ListItem Value="oops">oops</asp:ListItem>
                              <asp:ListItem Value="oops">oops</asp:ListItem>
                              <asp:ListItem Value="oops55">oops55</asp:ListItem>
                              <asp:ListItem Value="oop6">oop6</asp:ListItem>
                        </asp:ListBox>
                  </div>



Essentially, the scroll bars created here are actually the DIV scroll bars, and not the Listbox scrollbars. If the listbox is not tall enough (height), you will see TWO vertical scroll bars if you were to pan all the way over with the horizontal one. In the example I have given you, it does just that. To correct that, to prevent two vertical scroll bars, you have to set the height of the LISTBOX element (not the DIV element) to be tall enough to see all of the items. You also may want to consider re-sizing the LISTBOX on Page_Load to set the height to exactly the amount of items in the LISTBOX.

0
 

Author Comment

by:jenniferw
ID: 16993018
Thank´s for helping me out on this problem!
0
 
LVL 11

Expert Comment

by:five22bags
ID: 16993075
That is the auto-resize code.
You can use something similar to resize for the width; remember that the width of the ListBox WITHIN the DIV element must be wide enough to view the entire item text, whereas the width of the DIV can be as small as you want it (to create the horizontal scroll).

private void Page_Load(object sender, System.EventArgs e)
            {
                  if (!IsPostBack)
                  {
                        //Load all items in the list box.
                        int nItem = Convert.ToInt32(ListBox1.Items.Count * 17);
                        ListBox1.Height = nItem; //Set height depends on the font size.
                        ListBox1.Width = 800; //This will ensure the list item won't be shrinked!
                  }
            }
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

A short article about a problem I had getting the GPS LocationListener working.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

809 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