Set proportional ListView column widths - how to

  • Thread starter Thread starter Richard Muller
  • Start date Start date
R

Richard Muller

Hi All,

As I recall from Java Swing, I could assign the widths of columns to
something like a list control proportionally rather than absolutely. That
is, essentially I could assign "weights" x, y and z to the columns
respectively of a three-column list control; at run time, the system would
calculate the available space for the control and, given that w was
determined to be the absolute width of the control, would assign w*x /
(x+y+z) as the width of the first column, etc.

Is there a way to do this in C#?

--
TIA,
Richard

640K ought to be enough for anybody.
Bill Gates, 1981
 
I'm close to having the problem solved. I wrote the following
AdjustColumnWidths() function in my "formCV : System.Windows.Forms.Form"
class.

I invoked it just before the ResumeLayout statements at the end of the
"InitializeComponent" method.

It works almost perfectly. The only problem is that the rightmost column is
cut off slightly. I suspect that when I get the ListView's width, I'm
getting the window size rather than the client size. What can I do about
this?

In addition, a couple of improvements I need/want to make are:
1. the switch statement should be eliminated by using something like the
columns collection
2. I need to trap the change in window size and invoke an impoved version
that accomodates this.

void AdjustColumnWidths()

{

//

// Adjust column widths based on control width

//

int iListViewWidth = this.listView_Clients.Width;

int[] aiWeights = new int[]{90, 90, 20, 20};

int[] aiWidth = new int[aiWeights.Length];

int iTotWeight = 0;

foreach (int n in aiWeights)

{

iTotWeight = iTotWeight + n;

}

int indx = 0;

for (int i=0; i<aiWeights.Length; i++)

{

int w = iListViewWidth * aiWeights / iTotWeight;

i = i + 1;

switch (i)

{

case 1:

this.columnHeader1.Width = w; break;

case 2:

this.columnHeader2.Width = w; break;

case 3:

this.columnHeader3.Width = w; break;

case 4:

this.columnHeader4.Width = w; break;

}

}

}
 
The previous version I posted had a spurious statement incrementing "i" in
the second loop. It was left over from a previous looping approach and has
been removed now. In addition, I eliminated the select statement.

The main point is that this algorithm is does not produce precisely the
sizing required to have all columns visible in the space allocated. It
makes them five or ten percent wider than is warrented. I'd appreciate some
suggestions about how to ameliorate this situation.

TIA,

Richard



void AdjustColumnWidths()

{

//

// Adjust column widths based on control width

//

int iListViewWidth = this.listView_Clients.Width;

int[] aiWeights = new int[]{90, 90, 20, 20};

int[] aiWidth = new int[aiWeights.Length];

int iTotWeight = 0;

foreach (int n in aiWeights)

{

iTotWeight = iTotWeight + n;

}

int indx = 0;

for (int i=0; i<aiWeights.Length; i++)

{

int w = iListViewWidth * aiWeights / iTotWeight;

this.listView_Clients.Columns.Width = w;

}

}
 
Hi Morten,

Thanks for your comment. Do you have any suggestion on how I could identify
that element and somehow eliminate its effects from my calculation?

BTW, I posted (lower down on this thread) a revised function that eliminated
an error and simplified one section.

Regards,
Richard Muller
 
Hi Morten,

Thanks for your suggestion. Could you take a look at my code posted below
to see if you can suggest a change I might make to correct this problem?
The routine for computing the sizes of the four columns is
AdjustColumnWidths() at the bottom.

TIA,
Richard Muller

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace Complex_FormWithSplitters

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class formCV : System.Windows.Forms.Form

{

private System.Windows.Forms.Panel panel1;

private System.Windows.Forms.Splitter splitter1;

private System.Windows.Forms.RichTextBox richTextBox1;

private System.Windows.Forms.TabControl tabControl1;

private System.Windows.Forms.TabPage tabTechnologies;

private System.Windows.Forms.TabPage tabClients;

private System.Windows.Forms.TreeView treeView_Technologies;

private System.Windows.Forms.ListView listView_Clients;

private System.Windows.Forms.ColumnHeader columnHeader1;

private System.Windows.Forms.ColumnHeader columnHeader2;

private System.Windows.Forms.ColumnHeader columnHeader3;

private System.Windows.Forms.ColumnHeader columnHeader4;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public formCV()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#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()

{

System.Windows.Forms.ListViewItem listViewItem1 = new
System.Windows.Forms.ListViewItem(new
System.Windows.Forms.ListViewItem.ListViewSubItem[] {

new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "OCC",
System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window,
new System.Drawing.Font("Microsoft Sans Serif", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)))),

new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "Shared National
Credits (SNC) "),

new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "1999.10"),

new System.Windows.Forms.ListViewItem.ListViewSubItem(null,
"2000.10")}, -1);

System.Windows.Forms.ListViewItem listViewItem2 = new
System.Windows.Forms.ListViewItem(new string[] {

"Xerox",

"Xerox Dossier Publisher, a large client-server application",

"1997.10",

"1999.11"}, -1, System.Drawing.SystemColors.WindowText,
System.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans
Serif", 8.25F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))));

this.panel1 = new System.Windows.Forms.Panel();

this.tabControl1 = new System.Windows.Forms.TabControl();

this.tabTechnologies = new System.Windows.Forms.TabPage();

this.treeView_Technologies = new System.Windows.Forms.TreeView();

this.tabClients = new System.Windows.Forms.TabPage();

this.listView_Clients = new System.Windows.Forms.ListView();

this.columnHeader1 = new System.Windows.Forms.ColumnHeader();

this.columnHeader2 = new System.Windows.Forms.ColumnHeader();

this.columnHeader3 = new System.Windows.Forms.ColumnHeader();

this.columnHeader4 = new System.Windows.Forms.ColumnHeader();

this.splitter1 = new System.Windows.Forms.Splitter();

this.richTextBox1 = new System.Windows.Forms.RichTextBox();

this.panel1.SuspendLayout();

this.tabControl1.SuspendLayout();

this.tabTechnologies.SuspendLayout();

this.tabClients.SuspendLayout();

this.SuspendLayout();

//

// panel1

//

this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {

this.tabControl1});

this.panel1.Dock = System.Windows.Forms.DockStyle.Left;

this.panel1.Name = "panel1";

this.panel1.Size = new System.Drawing.Size(200, 365);

this.panel1.TabIndex = 0;

//

// tabControl1

//

this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {

this.tabTechnologies,

this.tabClients});

this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;

this.tabControl1.Name = "tabControl1";

this.tabControl1.SelectedIndex = 0;

this.tabControl1.Size = new System.Drawing.Size(200, 365);

this.tabControl1.TabIndex = 0;

//

// tabTechnologies

//

this.tabTechnologies.Controls.AddRange(new System.Windows.Forms.Control[] {

this.treeView_Technologies});

this.tabTechnologies.Location = new System.Drawing.Point(4, 22);

this.tabTechnologies.Name = "tabTechnologies";

this.tabTechnologies.Size = new System.Drawing.Size(192, 339);

this.tabTechnologies.TabIndex = 0;

this.tabTechnologies.Text = "Technologies";

//

// treeView_Technologies

//

this.treeView_Technologies.Dock = System.Windows.Forms.DockStyle.Fill;

this.treeView_Technologies.ImageIndex = -1;

this.treeView_Technologies.Name = "treeView_Technologies";

this.treeView_Technologies.Nodes.AddRange(new
System.Windows.Forms.TreeNode[] {

new System.Windows.Forms.TreeNode("C", new System.Windows.Forms.TreeNode[] {

new System.Windows.Forms.TreeNode("Project 1"),

new System.Windows.Forms.TreeNode("Project 2")}),

new System.Windows.Forms.TreeNode("C++", new System.Windows.Forms.TreeNode[]
{

new System.Windows.Forms.TreeNode("Project 1"),

new System.Windows.Forms.TreeNode("Project 2")}),

new System.Windows.Forms.TreeNode("Java"),

new System.Windows.Forms.TreeNode("Perl"),

new System.Windows.Forms.TreeNode("Ruby")});

this.treeView_Technologies.SelectedImageIndex = -1;

this.treeView_Technologies.Size = new System.Drawing.Size(192, 339);

this.treeView_Technologies.TabIndex = 0;

//

// tabClients

//

this.tabClients.Controls.AddRange(new System.Windows.Forms.Control[] {

this.listView_Clients});

this.tabClients.Location = new System.Drawing.Point(4, 22);

this.tabClients.Name = "tabClients";

this.tabClients.Size = new System.Drawing.Size(192, 339);

this.tabClients.TabIndex = 1;

this.tabClients.Text = "Clients";

//

// listView_Clients

//

this.listView_Clients.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {

this.columnHeader1,

this.columnHeader2,

this.columnHeader3,

this.columnHeader4});

this.listView_Clients.Dock = System.Windows.Forms.DockStyle.Fill;

this.listView_Clients.Items.AddRange(new System.Windows.Forms.ListViewItem[]
{

listViewItem1,

listViewItem2});

this.listView_Clients.Name = "listView_Clients";

this.listView_Clients.Size = new System.Drawing.Size(192, 339);

this.listView_Clients.TabIndex = 0;

this.listView_Clients.View = System.Windows.Forms.View.Details;

//

// columnHeader1

//

this.columnHeader1.Text = "Org. Name";

this.columnHeader1.Width = 90;

//

// columnHeader2

//

this.columnHeader2.Text = "From";

this.columnHeader2.Width = 90;

//

// columnHeader3

//

this.columnHeader3.Text = "To";

this.columnHeader3.Width = 30;

//

// columnHeader4

//

this.columnHeader4.Text = "Projects";

this.columnHeader4.Width = 30;

//

// splitter1

//

this.splitter1.Location = new System.Drawing.Point(200, 0);

this.splitter1.Name = "splitter1";

this.splitter1.Size = new System.Drawing.Size(3, 365);

this.splitter1.TabIndex = 1;

this.splitter1.TabStop = false;

//

// richTextBox1

//

this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;

this.richTextBox1.Location = new System.Drawing.Point(203, 0);

this.richTextBox1.Name = "richTextBox1";

this.richTextBox1.Size = new System.Drawing.Size(317, 365);

this.richTextBox1.TabIndex = 2;

this.richTextBox1.Text = "richTextBox1";

//

// formCV

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(520, 365);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.richTextBox1,

this.splitter1,

this.panel1});

this.Name = "formCV";

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Curriculum Vitae";

this.panel1.ResumeLayout(false);

this.tabControl1.ResumeLayout(false);

this.tabTechnologies.ResumeLayout(false);

this.tabClients.ResumeLayout(false);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new formCV());

}

void AdjustColumnWidths()

{

//

// Adjust column widths based on control width

//

int iListViewWidth = this.listView_Clients.Width;

int[] aiWeights = new int[]{90, 90, 20, 20};

int[] aiWidth = new int[aiWeights.Length];

int iTotWeight = 0;

foreach (int n in aiWeights)

{

iTotWeight = iTotWeight + n;

}

int indx = 0;

for (int i=0; i<aiWeights.Length; i++)

{

int w = iListViewWidth * aiWeights / iTotWeight;

this.listView_Clients.Columns.Width = w;

}

}

}

}
 
Back
Top