473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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[i].Width;
}

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

listView1.Columns[i].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
Apr 10 '07 #1
1 3529
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[i].Width;
}

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

On Tue, 10 Apr 2007 17:03:01 +0200, Kimmo Laine <re******@newsgroup.onlywrote:
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[i].Width;
}

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

listView1.Columns[i].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


--
Happy coding!
Morten Wennevik [C# MVP]
Apr 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: VM | last post by:
How can I fill up a listview with text file contents? My listview has two columns and the first column fills up with a while loop: while (myString != null) { myString = sr.Readline();...
2
by: Sean | last post by:
Hi all, I wonder if there is a way to prevent user from resizing the last column by moving the last vertical line on a ListView, meaning I don't want user to be able to move the last vertical...
0
by: cyrille | last post by:
Hello from example from web i did a little code to avoiding columnHeader resize. this code seems to work well, but when I put a 'normal' ListView on the same Form than my overrided ListView it...
4
by: Hunter Kirk | last post by:
I'm having a rather tedious problem with the listview control in visual basic.net I am trying to achive two simple procedures, I thought :) 1. I have only one column populated but still there...
2
by: Just Me | last post by:
Listview is Docked=Fill I set all column widths to -2 and it works as expected if the listview needs to be too wide for the window. That is, I get an H-scrollbar and the columns are the correct...
7
by: BobAchgill | last post by:
I am trying to decide which of these controls to use to implement letting my user select a full row from MyList. The MyList has several columns which would be nice to sort by at run time. The...
2
by: Thief_ | last post by:
Does the VB Listview have some sort of event which fires when the user resizes the columns in Details view? I want to record the column sizes as the user changes them. If not, can I create an event...
6
by: Maileen | last post by:
Hi, I have a listview in report mode. I have 3 columns in this listview and 1 column i would like to hide it from user's view. i tried to give width = 0 but user is still able to resize column...
0
by: erik.alsmyr | last post by:
When setting the Scrollable property of a listview and resizeing it the headers will be painted wrongly. This defect is documented in MSDN as: In versions of the .NET Framework prior to version...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.