Navigation

Search

Categories

On this page

How to remove the horizontal scrollbar from a ListView
Get the current username and domain for the currently authenticated SQL Server user for Windows Integrated Authentication
Memories from MSDN Unleashed: The Best of PDC event

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.

Comments [0] | | # 
# Monday, September 28, 2009
Monday, September 28, 2009 1:58:31 PM (Mountain Daylight Time, UTC-06:00) ( )

The title of this post declares the intent of the code below

DECLARE @Domain VARCHAR(50);
DECLARE @Account VARCHAR(50);
SET @Domain = SUBSTRING(SYSTEM_USER, 0, CHARINDEX('\', SYSTEM_USER));
SET @Account = SUBSTRING(SYSTEM_USER, CHARINDEX('\', SYSTEM_USER) + 1, LEN(SYSTEM_USER) - CHARINDEX('\', SYSTEM_USER));
SELECT @Domain + '\' +@Account AS samAccountName;

Comments [0] | | # 
# Wednesday, January 28, 2009
Wednesday, January 28, 2009 9:42:35 AM (Mountain Standard Time, UTC-07:00) ( All things Microsoft )
I went to the MSDN Unleashed: The Best of PDC event at ConfigureSoft. David Yack and Rob Bagby were presenting on the topics originally presented at PDC
  • What to expect with C# 4.0
    • It's really just the distilled content that has been available since October. You could hear it straight from Anders himself. Dave did an awesome job though.
  • The Silverlight Control Toolkit
    • Microsoft is releasing code out of band for Silverlight for those who wish to live on the edge
  • REST with WCF and the WCF REST Starter Kit
    • Creating a REST-ful web service is dead simple using WCF. Add to it the starter kit and you get all sorts of free goodies out of the box to making even more dead simple. Once again, "It Just Works"
  • Overview of Windows Azure (“The Cloud OS”) and Azure Services
    • Azure = a cloud operating system + a set of developer services. Somethings you just gotta have a platform to write scalable apps on without worrying about the infrastructure.
We got some awesome "Rest in WCF" t-shirts with a photo of Rob's dog on it. I'm going to have to hang it on the wall so everyone will ask why I have a sleeping dog on a shirt. It's all because of Representational State Transfer of course!

Rob had some very funny lines I will quote often in the future as well.

Like the guy on the Ginsu knife commercial says, But that's not all you get
...that's a potentially marriage limiting maneuver...
Thanks for the great presentation gentlemen. Thanks to Microsoft for continuing these events. They're a great way to digest the information.
Comments [29] | | #