ListView - Fit to width of column

  • Thread starter Thread starter Palm Kevin
  • Start date Start date
P

Palm Kevin

I need your help.
I have a ListView and I would like to fit the width of the columns to
their content.

I found at the microsoft-page and at many other pages that :
"To adjust the width of the longest item in the column, set the Width
property to -1. To autosize to the width of the column heading, set
the Width property to -2."

But it seems not to work with the .net Framework !?!

When I initialize the columnheader-with with a negativ number, than
the width is always adjusted to the header, but bever to the
content...

Can anybody confirm that the "content-auto-resize" does not work for
the .net framework? Or has anybody a working example?

I really need your help!
I searched all the day to find a solution, but I didn't find one.

Thanks,
Palm Kevin
 
It works, just set the width to -1 when the listview is filled with data.
Matteo.
 
hi,
calculate the width of cell text before adding it and set
the column width with the calculated value. formula is as
follows:

Graphics g = control.CreateGraphics();
colWidth = (int)g.MeasureString(text,
ListView.Font).Width;

regards
Arindrajit
 
It works, just set the width to -1 when the listview is filled with data.

I'm finding that this doesn't make any difference. -1 does the same thing
as -2. I could use the MeasureString technique posted above but would like
to avoid that overhead as I would have to measure each string in every cell
and then determine which is the longest in each column.
 
Before adding rows : When I initialize the column width of the listView
with -1 or -2 then the width of the column is always fidden to the
header length, never to the content length.

After adding rows : When I initialize the column width of the listView
with -1 or -2 then the width of the column is always 0.

This seems not to work.

I will try now the MeasureString technique, and I hope that the overhead
is not too important...

Thank you for your answers,
Kevin
 
Before adding rows : When I initialize the column width of the listView
with -1 or -2 then the width of the column is always fidden to the
header length, never to the content length.

After adding rows : When I initialize the column width of the listView
with -1 or -2 then the width of the column is always 0.

This seems not to work.

I will try now the MeasureString technique, and I hope that the overhead
is not too important...

Thank you for your answers,
Kevin
 
Oups, I dind't want to post this message twice ...

I wrote a function that resizes the columns using the MeaureString
method.
This works very good (It don't take up much time because my lists are
not very large.

But if anybody has a suggestion how I can resize the columns without
measuring the length of all strings .... I'm waiting :)

Palm Kevin
 
Hello Kevin,

Could you please send a simple application that would reproduce the problem?

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: Palm Kevin <[email protected]>
| References: <#[email protected]>
| X-Newsreader: AspNNTP 1.50 (ActionJackson.com)
| Subject: Re: ListView - Fit to width of column
| Mime-Version: 1.0
| Content-Type: text/plain; charset="us-ascii"
| Content-Transfer-Encoding: 7bit
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Date: Mon, 08 Sep 2003 02:16:17 -0700
| NNTP-Posting-Host: actionjackson133.dsl.frii.net 216.17.147.133
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:32967
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Oups, I dind't want to post this message twice ...
|
| I wrote a function that resizes the columns using the MeaureString
| method.
| This works very good (It don't take up much time because my lists are
| not very large.
|
| But if anybody has a suggestion how I can resize the columns without
| measuring the length of all strings .... I'm waiting :)
|
| Palm Kevin
|
|
| Don't just participate in USENET...get rewarded for it!
|
 
Here a simple smart-device-application that reproduces the problem :
(Column-width fits to columnheader instead of content).

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SmartDeviceApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
InitializeComponent();
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.MinimizeBox = false;
this.Controls.Add(generateTablePanel(this.Size,new string [,]{
{"","Top casse","Montant casse","Quantités","Ventes (qtitées)"},
{"Art1","Mini roulettes 250","9,72","6,00","22,00"},
{"Art2","CO Choc'n choc","10,53","3,00","6,00"},
{"Art3","Meurisse tablet","3,51","3,00","25,00"},
{"Art4","C'or chokotoff","5,60","2,00","31,00"},
{"Art5","Kinder delice","5,24","2,00","61,00"}}));
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private Panel generateTablePanel(Size panelSize,String[,] table)
{
Panel p = new Panel();
p.Size = panelSize;
// Create a new ListView control.
ListView listView1 = new ListView();

// Set the view to show details.
listView1.View = View.Details;

int columnCount = table.GetLength(1);
int rowCount = table.GetLength(0);

listView1.Size = Size;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;

// Create columns for the items and subitems.
for(int column = 0; column < columnCount;column ++)
{
listView1.Columns.Add(table[0,column],-1,HorizontalAlignment.Left);
}

// Create items and sets of subitems for each item.
ListViewItem item;
for(int row = 1; row < rowCount; row++)
{
item = new ListViewItem(table[row,0]);
for(int column = 1; column < columnCount;column ++)
item.SubItems.Add(table[row,column]);
listView1.Items.Add(item);
}

p.Controls.Add(listView1);
return p;
}
}
}
 
This is a known issue in the .NET Compact Framework v1.0. It was fixed in
Service Pack1.

Please note that, when you use SP1, you will need to set the Width property
to -1 after adding all items to the ListView.

Hope this helps.
Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: Palm Kevin <[email protected]>
| References: <Xu6#[email protected]>
| X-Newsreader: AspNNTP 1.50 (ActionJackson.com)
| Subject: Re: ListView - Fit to width of column
| Mime-Version: 1.0
| Content-Type: text/plain; charset="us-ascii"
| Content-Transfer-Encoding: 7bit
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Date: Tue, 16 Sep 2003 00:26:27 -0700
| NNTP-Posting-Host: actionjackson133.dsl.frii.net 216.17.147.133
| Lines: 1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:33462
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Here a simple smart-device-application that reproduces the problem :
| (Column-width fits to columnheader instead of content).
|
| using System;
| using System.Drawing;
| using System.Windows.Forms;
|
| namespace SmartDeviceApplication1
| {
| /// <summary>
| /// Summary description for Form1.
| /// </summary>
| public class Form1 : System.Windows.Forms.Form
| {
| private System.Windows.Forms.MainMenu mainMenu1;
|
| public Form1()
| {
| InitializeComponent();
| }
|
| #region Windows Form Designer generated code
| /// <summary>
| /// Required method for Designer support - do not modify
| /// the contents of this method with the code editor.
| /// </summary>
| private void InitializeComponent()
| {
| this.mainMenu1 = new System.Windows.Forms.MainMenu();
| this.Menu = this.mainMenu1;
| this.Text = "Form1";
| this.MinimizeBox = false;
| this.Controls.Add(generateTablePanel(this.Size,new string [,]{
| {"","Top casse","Montant casse","Quantités","Ventes (qtitées)"},
| {"Art1","Mini roulettes 250","9,72","6,00","22,00"},
| {"Art2","CO Choc'n choc","10,53","3,00","6,00"},
| {"Art3","Meurisse tablet","3,51","3,00","25,00"},
| {"Art4","C'or chokotoff","5,60","2,00","31,00"},
| {"Art5","Kinder delice","5,24","2,00","61,00"}}));
| }
| #endregion
|
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
|
| static void Main()
| {
| Application.Run(new Form1());
| }
|
| private Panel generateTablePanel(Size panelSize,String[,] table)
| {
| Panel p = new Panel();
| p.Size = panelSize;
| // Create a new ListView control.
| ListView listView1 = new ListView();
|
| // Set the view to show details.
| listView1.View = View.Details;
|
| int columnCount = table.GetLength(1);
| int rowCount = table.GetLength(0);
|
| listView1.Size = Size;
| // Select the item and subitems when selection is made.
| listView1.FullRowSelect = true;
|
| // Create columns for the items and subitems.
| for(int column = 0; column < columnCount;column ++)
| {
| listView1.Columns.Add(table[0,column],-1,HorizontalAlignment.Left);
| }
|
| // Create items and sets of subitems for each item.
| ListViewItem item;
| for(int row = 1; row < rowCount; row++)
| {
| item = new ListViewItem(table[row,0]);
| for(int column = 1; column < columnCount;column ++)
| item.SubItems.Add(table[row,column]);
| listView1.Items.Add(item);
| }
|
| p.Controls.Add(listView1);
| return p;
| }
| }
| }
|
|
|
|
|
| Don't just participate in USENET...get rewarded for it!
|
 
Thank you! It works with the Service Pack 1!

The Service Pack was already installed it before, but apparently, it was
not installed correctly ...

As mentionned in a previous post, I already had written a function that
auto-resize the column. This function calculates the length of all
strings (even the header-string !!) and initializes the column to the
width of the longest text. Doing so, has the advantage that the column
is fitted to the longest string: Header OR Content.
When using the autoresize methods of the ListView-object (with = -1 or
width = -2), then you can fit the width of the columns either to the
content OR the header, but never on the longest!

Is there a possiblity to fit the column-width to the LONGEST label
(Header OR Content) ?

If not, I'll continue to use my little function ...

Thanks,
Palm Kevin
 
You can set width to -2. This will fit the column width to header text or
the longest item.

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: Palm Kevin <[email protected]>
| References: <[email protected]>
| X-Newsreader: AspNNTP 1.50 (ActionJackson.com)
| Subject: Re: ListView - Fit to width of column
| Mime-Version: 1.0
| Content-Type: text/plain; charset="us-ascii"
| Content-Transfer-Encoding: 7bit
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Date: Thu, 18 Sep 2003 02:17:02 -0700
| NNTP-Posting-Host: actionjackson133.dsl.frii.net 216.17.147.133
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:33828
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Thank you! It works with the Service Pack 1!
|
| The Service Pack was already installed it before, but apparently, it was
| not installed correctly ...
|
| As mentionned in a previous post, I already had written a function that
| auto-resize the column. This function calculates the length of all
| strings (even the header-string !!) and initializes the column to the
| width of the longest text. Doing so, has the advantage that the column
| is fitted to the longest string: Header OR Content.
| When using the autoresize methods of the ListView-object (with = -1 or
| width = -2), then you can fit the width of the columns either to the
| content OR the header, but never on the longest!
|
| Is there a possiblity to fit the column-width to the LONGEST label
| (Header OR Content) ?
|
| If not, I'll continue to use my little function ...
|
| Thanks,
| Palm Kevin
|
|
| Don't just participate in USENET...get rewarded for it!
|
 
Back
Top