473,549 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Colum ns.Add("Col 1");
listView1.Colum ns[0].Width = 100;

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

listView1.Heade rStyle = ColumnHeaderSty le.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.Colu mns.Count - 1); i++) {
w += listView1.Colum ns[i].Width;
}

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

listView1.Colum ns[i].Width = listView1.Clien tRectangle.Widt h - 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 3542
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.Co lumns.Count 0)
{
int w = 0;
for (int i = 0; i < listView1.Colum ns.Count - 1; i++)
{
w += listView1.Colum ns[i].Width;
}

int lastIndex = listView1.Colum ns.Count - 1;
listView1.Colum ns[lastIndex].Width = listView1.Clien tSize.Width - w;
}

On Tue, 10 Apr 2007 17:03:01 +0200, Kimmo Laine <re******@newsg roup.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.Colum ns.Add("Col 1");
listView1.Colum ns[0].Width = 100;

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

listView1.Heade rStyle = ColumnHeaderSty le.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.Colu mns.Count - 1); i++) {
w += listView1.Colum ns[i].Width;
}

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

listView1.Colum ns[i].Width = listView1.Clien tRectangle.Widt h - 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
4621
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(); listView1.Items.Add (myString); } Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems...
2
3308
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 line and change the width of the last column on a ListView. Any suggestion would be greatly appreciated. Thank you in advance.
0
4486
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 seems to stop working. if I'm resizing 'normal' ListView ColumnHeaders, then I can resizing ColumnHeaders of my overrided ListView. If I don't...
4
1773
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 is an extra empty column listed to the right of the header. Is there no way to specify that I only want a single column. Or is there a way to hide...
2
2514
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 size. However, if the window (and thus the listview) is wider that the listview needs to be it makes the last column extra wide so that it fills...
7
14961
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 MyList data is resident in a dataset table. I'm stuck and can't choose either because. If I choose ListView as my control I don't understand how...
2
2291
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 like this? -- | +-- Thief_ |
6
3021
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 after in runtime mode. So, could someone help me please ? thx, Maileen
0
1152
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 2.0, the column headers were not painted correctly when setting this property to false and resizing the control to make it larger. To work around...
0
7524
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7960
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.