473,796 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set proportional ListView column widths - how to

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
Nov 15 '05 #1
5 5228
I'm close to having the problem solved. I wrote the following
AdjustColumnWid ths() function in my "formCV : System.Windows. Forms.Form"
class.

I invoked it just before the ResumeLayout statements at the end of the
"InitializeComp onent" 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 AdjustColumnWid ths()

{

//

// Adjust column widths based on control width

//

int iListViewWidth = this.listView_C lients.Width;

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

int[] aiWidth = new int[aiWeights.Lengt h];

int iTotWeight = 0;

foreach (int n in aiWeights)

{

iTotWeight = iTotWeight + n;

}

int indx = 0;

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

{

int w = iListViewWidth * aiWeights[i] / iTotWeight;

i = i + 1;

switch (i)

{

case 1:

this.columnHead er1.Width = w; break;

case 2:

this.columnHead er2.Width = w; break;

case 3:

this.columnHead er3.Width = w; break;

case 4:

this.columnHead er4.Width = w; break;

}

}

}





Nov 15 '05 #2
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 AdjustColumnWid ths()

{

//

// Adjust column widths based on control width

//

int iListViewWidth = this.listView_C lients.Width;

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

int[] aiWidth = new int[aiWeights.Lengt h];

int iTotWeight = 0;

foreach (int n in aiWeights)

{

iTotWeight = iTotWeight + n;

}

int indx = 0;

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

{

int w = iListViewWidth * aiWeights[i] / iTotWeight;

this.listView_C lients.Columns[i].Width = w;

}

}
Nov 15 '05 #3
My guess is that a horizontal bar might be messing up your sizing.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #4
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

"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:oprxuf9caa hntkfz@localhos t...
My guess is that a horizontal bar might be messing up your sizing.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 15 '05 #5
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
AdjustColumnWid ths() at the bottom.

TIA,
Richard Muller

using System;

using System.Drawing;

using System.Collecti ons;

using System.Componen tModel;

using System.Windows. Forms;

using System.Data;

namespace Complex_FormWit hSplitters

{

/// <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.RichTextB ox richTextBox1;

private System.Windows. Forms.TabContro l tabControl1;

private System.Windows. Forms.TabPage tabTechnologies ;

private System.Windows. Forms.TabPage tabClients;

private System.Windows. Forms.TreeView treeView_Techno logies;

private System.Windows. Forms.ListView listView_Client s;

private System.Windows. Forms.ColumnHea der columnHeader1;

private System.Windows. Forms.ColumnHea der columnHeader2;

private System.Windows. Forms.ColumnHea der columnHeader3;

private System.Windows. Forms.ColumnHea der columnHeader4;

/// <summary>

/// Required designer variable.

/// </summary>

private System.Componen tModel.Containe r components = null;

public formCV()

{

//

// Required for Windows Form Designer support

//

InitializeCompo nent();

//

// TODO: Add any constructor code after InitializeCompo nent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Disp ose();

}

}

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 InitializeCompo nent()

{

System.Windows. Forms.ListViewI tem listViewItem1 = new
System.Windows. Forms.ListViewI tem(new
System.Windows. Forms.ListViewI tem.ListViewSub Item[] {

new System.Windows. Forms.ListViewI tem.ListViewSub Item(null, "OCC",
System.Drawing. SystemColors.Wi ndowText, System.Drawing. SystemColors.Wi ndow,
new System.Drawing. Font("Microsoft Sans Serif", 8.25F,
System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)))),

new System.Windows. Forms.ListViewI tem.ListViewSub Item(null, "Shared National
Credits (SNC) "),

new System.Windows. Forms.ListViewI tem.ListViewSub Item(null, "1999.10"),

new System.Windows. Forms.ListViewI tem.ListViewSub Item(null,
"2000.10")} , -1);

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

"Xerox",

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

"1997.10",

"1999.11"}, -1, System.Drawing. SystemColors.Wi ndowText,
System.Drawing. SystemColors.Wi ndow, new System.Drawing. Font("Microsoft Sans
Serif", 8.25F, System.Drawing. FontStyle.Regul ar,
System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0))));

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

this.tabControl 1 = new System.Windows. Forms.TabContro l();

this.tabTechnol ogies = new System.Windows. Forms.TabPage() ;

this.treeView_T echnologies = new System.Windows. Forms.TreeView( );

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

this.listView_C lients = new System.Windows. Forms.ListView( );

this.columnHead er1 = new System.Windows. Forms.ColumnHea der();

this.columnHead er2 = new System.Windows. Forms.ColumnHea der();

this.columnHead er3 = new System.Windows. Forms.ColumnHea der();

this.columnHead er4 = new System.Windows. Forms.ColumnHea der();

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

this.richTextBo x1 = new System.Windows. Forms.RichTextB ox();

this.panel1.Sus pendLayout();

this.tabControl 1.SuspendLayout ();

this.tabTechnol ogies.SuspendLa yout();

this.tabClients .SuspendLayout( );

this.SuspendLay out();

//

// panel1

//

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

this.tabControl 1});

this.panel1.Doc k = System.Windows. Forms.DockStyle .Left;

this.panel1.Nam e = "panel1";

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

this.panel1.Tab Index = 0;

//

// tabControl1

//

this.tabControl 1.Controls.AddR ange(new System.Windows. Forms.Control[] {

this.tabTechnol ogies,

this.tabClients });

this.tabControl 1.Dock = System.Windows. Forms.DockStyle .Fill;

this.tabControl 1.Name = "tabControl 1";

this.tabControl 1.SelectedIndex = 0;

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

this.tabControl 1.TabIndex = 0;

//

// tabTechnologies

//

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

this.treeView_T echnologies});

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

this.tabTechnol ogies.Name = "tabTechnologie s";

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

this.tabTechnol ogies.TabIndex = 0;

this.tabTechnol ogies.Text = "Technologi es";

//

// treeView_Techno logies

//

this.treeView_T echnologies.Doc k = System.Windows. Forms.DockStyle .Fill;

this.treeView_T echnologies.Ima geIndex = -1;

this.treeView_T echnologies.Nam e = "treeView_Techn ologies";

this.treeView_T echnologies.Nod es.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_T echnologies.Sel ectedImageIndex = -1;

this.treeView_T echnologies.Siz e = new System.Drawing. Size(192, 339);

this.treeView_T echnologies.Tab Index = 0;

//

// tabClients

//

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

this.listView_C lients});

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_Client s

//

this.listView_C lients.Columns. AddRange(new
System.Windows. Forms.ColumnHea der[] {

this.columnHead er1,

this.columnHead er2,

this.columnHead er3,

this.columnHead er4});

this.listView_C lients.Dock = System.Windows. Forms.DockStyle .Fill;

this.listView_C lients.Items.Ad dRange(new System.Windows. Forms.ListViewI tem[]
{

listViewItem1,

listViewItem2}) ;

this.listView_C lients.Name = "listView_Clien ts";

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

this.listView_C lients.TabIndex = 0;

this.listView_C lients.View = System.Windows. Forms.View.Deta ils;

//

// columnHeader1

//

this.columnHead er1.Text = "Org. Name";

this.columnHead er1.Width = 90;

//

// columnHeader2

//

this.columnHead er2.Text = "From";

this.columnHead er2.Width = 90;

//

// columnHeader3

//

this.columnHead er3.Text = "To";

this.columnHead er3.Width = 30;

//

// columnHeader4

//

this.columnHead er4.Text = "Projects";

this.columnHead er4.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.richTextBo x1.Dock = System.Windows. Forms.DockStyle .Fill;

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

this.richTextBo x1.Name = "richTextBo x1";

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

this.richTextBo x1.TabIndex = 2;

this.richTextBo x1.Text = "richTextBo x1";

//

// formCV

//

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

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

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

this.richTextBo x1,

this.splitter1,

this.panel1});

this.Name = "formCV";

this.StartPosit ion = System.Windows. Forms.FormStart Position.Center Screen;

this.Text = "Curriculum Vitae";

this.panel1.Res umeLayout(false );

this.tabControl 1.ResumeLayout( false);

this.tabTechnol ogies.ResumeLay out(false);

this.tabClients .ResumeLayout(f alse);

this.ResumeLayo ut(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run (new formCV());

}

void AdjustColumnWid ths()

{

//

// Adjust column widths based on control width

//

int iListViewWidth = this.listView_C lients.Width;

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

int[] aiWidth = new int[aiWeights.Lengt h];

int iTotWeight = 0;

foreach (int n in aiWeights)

{

iTotWeight = iTotWeight + n;

}

int indx = 0;

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

{

int w = iListViewWidth * aiWeights[i] / iTotWeight;

this.listView_C lients.Columns[i].Width = w;

}

}

}

}
Nov 15 '05 #6

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

Similar topics

1
1484
by: sho_nuff | last post by:
Hello all, Is it possible to have column specific context menus? so, if i have three columns containing different objects / info, i could have a context menu corresponding to each one? Thanks, SN
18
1523
by: Menu Chen | last post by:
First I feel very sorry that my English is very poor.So Please Don't be boring. I have asked this question here once ,but no one could give me answer,and the problem still bothers me. I use a ListView Control in my programs.You know that we always need a scroll bar in this contorl.But All I need is the vertical scroll bar.I don't want the control display horizontal scroll bar to Users,but how to do this?
0
1145
by: CR | last post by:
I'm having a problem with the Listview control. I populated a Listview with 4 columns. On my PC they show up just fine, the right hand edge of the 4th column lines up with the right hand edge of the Listview control. However when I tried to run it on another PC you could only see 3 columns in the Listview, you have to scroll to see the 4th. I figured out it was because I had my screen property set to large fonts (Control...
9
10393
by: Kevin Westhead | last post by:
Can anyone confirm whether or not there are any limits imposed on the widths of autosized columns in a list-view. I've found that the autosizing appears to have an upper limit, i.e. it will not expand beyond some (unknown) maximum length. This applies to user-autosizing (double-clicking the divider) and code-autosizing using LVSCW_AUTOSIZE. For example, create a new project and place a ListView (VB5 version) and CommandButton on a form...
2
2523
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 the right side of the window.
12
7488
by: J L | last post by:
When I fill a listview, I resize the columns to fit the data. I need to know if the data will fit vertically or if there will be a vertical scroll bar. I need to know this so I can allow for it on the overall size of the listview. My question therefore is, how can I tell if the items I have added will fit in the listview at its given height? A secondary one, just for interest sake...is there a way to determine the exact heght needed...
4
408
by: Jeremy | last post by:
I am a VB6 programming trying to make the jump to VS2005... I have a ListView control, and have manipulated the column widths in the design view, but the column widths do not change in runtime. Can anyone help with this?
4
6094
by: gene kelley | last post by:
I had a VB6 app that used a third party ListView control to simply display various recordsets. The control had a VirtualMode which I used for this display. Now in VB2005, I want to do the same with the native ListView to display various datasets in VirtualMode. I have a ListView with the VirtualListSize set and the appropriate columns in place which is all similar to the old VB6 app. However, the similarities end there as I can't find...
0
1256
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, First, thanks to everyone that has answered my many questions it the past. It has helped greatly! Now, I'm using VS 2005 Pro and I have a standard ListView control on a form and it contains 4 columns and 4 rows. I've selected the 10pt Courier New font. When I'm in Designer I can stretch the column widths as wide as I want. However, when I run the application it makes them as wide as it wants and completely ignores the widths...
0
9684
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.