Question about ListView - resizing the last column: why does this work

K

Kimmo Laine

Hi!

[NET 2.0, VS 2005, XP SP2]

I need to resize the last column in my listview control so that there won´t
be horizontal scrollbar.

Lets first create lv and add some items:

listView1.View = View.Details;

listView1.Columns.Add("Col 1");
listView1.Columns[0].Width = 100;

listView1.Columns.Add("Col 2");
listView1.Columns[1].Width = 200;

listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
listView1.Size = new Size(200,60);

for (int i = 0; i < 10; i++) {
listView1.Items.Add(i.ToString());
}

Now, without executing this you can see that lv is too small to hold the
items - both scrollbars will be added.
Vertical scrollbar is ok, but i don´t want the horizontal one.

So, lets add the following code:

int w = 0;
byte i = 0;
for (; i < (listView1.Columns.Count - 1); i++) {
w += listView1.Columns.Width;
}

if (listView1.Items != null && listView1.Items.Count > 0) {
//int a = listView1.Items[0].Bounds.Height;
}

listView1.Columns.Width = listView1.ClientRectangle.Width - w;

By adding this, the last column will become narrow but not enough.

If i uncomment the last line - it works exactly as it should: the last
column will use all the remaining space that isn´t left by other columns and
the vertical scrollbar.

Here comes my question: why is this working? Is it safe to use this?


thx!
Kimmo
 
M

Morten Wennevik [C# MVP]

Hi Kimmo,

No need to adjust for the scrollbar, as the ClientSize/Rectangle is adjusted for that already.
If your goal is to set the width of the last column to whatever remains of the client area your code should work just fine, although it assumes the available space left is at least the minimum width of the last column. If the available space is less than this, you won't be able to display the last column at all, or prevent the scrollbar.

It will crash if there are no columns to adjust, though, so a check for this would be good.

if(listView1.Columns.Count > 0)
{
int w = 0;
for (int i = 0; i < listView1.Columns.Count - 1; i++)
{
w += listView1.Columns.Width;
}

int lastIndex = listView1.Columns.Count - 1;
listView1.Columns[lastIndex].Width = listView1.ClientSize.Width - w;
}

Hi!

[NET 2.0, VS 2005, XP SP2]

I need to resize the last column in my listview control so that there won´t
be horizontal scrollbar.

Lets first create lv and add some items:

listView1.View = View.Details;

listView1.Columns.Add("Col 1");
listView1.Columns[0].Width = 100;

listView1.Columns.Add("Col 2");
listView1.Columns[1].Width = 200;

listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
listView1.Size = new Size(200,60);

for (int i = 0; i < 10; i++) {
listView1.Items.Add(i.ToString());
}

Now, without executing this you can see that lv is too small to hold the
items - both scrollbars will be added.
Vertical scrollbar is ok, but i don´t want the horizontal one.

So, lets add the following code:

int w = 0;
byte i = 0;
for (; i < (listView1.Columns.Count - 1); i++) {
w += listView1.Columns.Width;
}

if (listView1.Items != null && listView1.Items.Count > 0) {
//int a = listView1.Items[0].Bounds.Height;
}

listView1.Columns.Width = listView1.ClientRectangle.Width - w;

By adding this, the last column will become narrow but not enough.

If i uncomment the last line - it works exactly as it should: the last
column will use all the remaining space that isn´t left by other columns and
the vertical scrollbar.

Here comes my question: why is this working? Is it safe to use this?


thx!
Kimmo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top