Navigation

Search

Categories

On this page

How to remove the horizontal scrollbar from a ListView

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 120
This Year: 1
This Month: 0
This Week: 0
Comments: 40

Sign In
Pick a theme:

# Thursday, October 01, 2009
Thursday, October 01, 2009 10:05:19 AM (Mountain Daylight Time, UTC-06:00) ( All things Microsoft | Code )

When you have a ListView control in Windows Forms, and you don't want the horizontal scroll bar to show up, you need to set the size of the columns to be the same 'Width' as the ListView.
What exactly is the 'Width' you should use here? Yes careful reader, I've been quoting Width because we need to take a closer look at what's going on.
Lets look at a dead simple example to clearly illustrate why this is a concern.

When I create a new Windows Forms application in Visual Studio, I get a standard sized form. Dragging a ListView control from the toolbox will create a control with the dimensions defined as

private void InitializeComponent()
{
   ...
   this.listView1.Size = new System.Drawing.Size(121, 97);
   ...
}
I added the following code to constructor of the Form after the InitializeComponent() call;
void Form1()
{
   InitializeComponent();
   
   listView1.Columns.Add("Name");
   for (int i = 0; i < 20; i++)
   {
      listView1.Items.Add(new string((char)(i + 33), 10));
   }
   // Naive width
   listView1.Columns[0].Width = listView1.Width;
}

What we end up with looks like the image below.

This is definately not what we wanted. Even though none of my data is forcing my scroll behavior, my column definition definitely is. I could change this to use the ClientSize of the Control, I know that's smaller. The documentation on MSDN for Control.ClientSize makes these remarks about the property.

"The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus..."
This will work right? I'll just make a quick change to the code, recompile and... drat. No love.
    // Setting the width on the Column
   //listView1.Columns[0].Width = listView1.Width;
   //listView1.Columns[0].Width = listView1.ClientSize.Width;

}

What is going on here?!
As it turns out, the calculation for ClientSize in the constructor is completely correct. The control hasn't been drawn yet, and it has no knowledge of whether it will need to use a vertical scrollbar to display the data. That's just fine control. I know how to fix you. I know how wide a scroll bar is, and I can figure that out on my own... Just watch me.

   // Setting the width on the Column
   //listView1.Columns[0].Width = listView1.Width;
   //listView1.Columns[0].Width = listView1.ClientSize.Width;
   listView1.Columns[0].Width = listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth;
}

Ha! Take that ListView. Now my column displays perfectly, and I have no Horizontal ScrollBar to contend with. Just to prove it to you, I'm going to check how wide it should be after the form shows up, to make sure I'm right...

      //listView1.Columns[0].Width = listView1.Width;
   //listView1.Columns[0].Width = listView1.ClientSize.Width;
   Debug.Print("ListView is {0} px wide and has a client width of {1} px", listView1.Width, listView1.ClientSize.Width);
   Debug.Print("VerticalScrollBarWidth: {0}", SystemInformation.VerticalScrollBarWidth);
   Debug.Print("Calculated width: {0}", listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth);
   listView1.Columns[0].Width = listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth;
   this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
   Debug.Print("Loaded width: {0}", listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth);
}
Debug Output:
ListView is 121 px wide and has a client width of 117 px
VerticalScrollBarWidth: 17
Calculated width: 100
Loaded width: 100

Uh-oh. I have a bad feeling. It seems like the Width of the ClientSize after the control has been painted is different than before it was painted. Could it be that when the VerticalScrollBar is painted, the control does in fact know how much room it has left to use to paint on the screen?! Isn't that in fact, exactly what the Remarks of the ClientSize property told me? Do the numbers above not in fact make perfect sense!? Is 117 - 17 = 100?! Yes... sigh. Perhaps I should have meditated on the implications that the remarks on MSDN were accurate from the begging.

"The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus..."

As it turns out, the correct time to set the width of a ListView Column is AFTER you've determined that there will be scrollbars painted on the control.