Set proportional ListView column widths - how to

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Muller

    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


  • Richard Muller

    #2
    A function that pretty much solves the problem

    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;

    }

    }

    }













    Comment

    • Richard Muller

      #3
      A corrected and improved sizing function

      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;

      }

      }


      Comment

      • Morten Wennevik

        #4
        Re: A function that pretty much solves the problem

        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/

        Comment

        • Richard Muller

          #5
          Re: A function that pretty much solves the problem

          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" <MortenWennevik @hotmail.com> wrote in message
          news:oprxuf9caa hntkfz@localhos t...[color=blue]
          > 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/[/color]


          Comment

          • Richard Muller

            #6
            Re: A function that pretty much solves the problem

            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;

            }

            }

            }

            }


            Comment

            Working...