0

I have a listview object that I want to have 2 columns. I want the two columns to stretch across the width of the listview and to have both column headers centered in each column. If I set the size of the columns to -2 (auto size), I get the following result (code followed by screenshot):

        this.recipList.Location = new System.Drawing.Point(16, 32);
        this.recipList.Name = "recipList";
        this.recipList.Size = new System.Drawing.Size(376, 296);
        this.recipList.TabIndex = 1;
        this.recipList.UseCompatibleStateImageBehavior = false;
        this.recipList.View = System.Windows.Forms.View.Details;
        this.recipList.Columns.Add("Recipient", -2, System.Windows.Forms.HorizontalAlignment.Center);
        this.recipList.Columns.Add("Number of Reports", -2, System.Windows.Forms.HorizontalAlignment.Center);

enter image description here

If I drag the columns manually within this window to mimic what I WANT to have, I get this. the only issue here is that even with Recipient set to HorizontalAlignment.Center it still left-aligns after I drag it to where I want it:

enter image description here

At this point I figure that the way to get both columns to span across the entire width of the listview box and for each column to take up half of it, I simply need to set the size of each column to 1/2 of the width of the entire listview. So I do this:

        this.recipList.Location = new System.Drawing.Point(16, 32);
        this.recipList.Name = "recipList";
        this.recipList.Size = new System.Drawing.Size(376, 296);
        this.recipList.TabIndex = 1;
        this.recipList.UseCompatibleStateImageBehavior = false;
        this.recipList.View = System.Windows.Forms.View.Details;
        this.recipList.Columns.Add("Recipient", 188, System.Windows.Forms.HorizontalAlignment.Center);
        this.recipList.Columns.Add("Number of Reports", 188, System.Windows.Forms.HorizontalAlignment.Center);

Since 188 is 1/2 of 376. This produces the following result:

enter image description here

As you can see, it almost worked with 2 issues.

  1. The Number of Reports column spans off of the listview box and creates a scrollbar. I don't want this.
  2. Recipient is still left-aligned even though it is set to HorizontalAlignment.Center.

Is there a better way to approach these two issues?

Giardino
  • 1,367
  • 3
  • 10
  • 30

2 Answers2

0

first of all you can create Headers for your ListView :

ColumnHeader headerFirst;
ColumnHeader headerSecond;
headerFirst = new ColumnHeader();
headerSecond = new ColumnHeader();

// Set the text, alignment and width for each column header.
headerFirst.Text = "Recipient";
headerFirst.TextAlign = HorizontalAlignment.Left;
headerFirst.Width = 188;

headerSecond.TextAlign = HorizontalAlignment.Left;
headerSecond.Text = "Number of Reports";
headerSecond.Width = 188;

// Add the headers to the ListView control.
ListView1.Columns.Add(headerFirst);
ListView1.Columns.Add(headerSecond);

It's explain on the MSDN Page.

0

The Width is the outer width of the ListView and includes the border pixels. Count them for your borderstyle and make your columns a bit smaller. Or if you want to do it right use the ClientSize property See here

The first column in a ListView is left aligned by design afaik; so to simulate a centered header you could pad it with spaces to the left. To do this in a precise manner you will have to measure the Text length using the given Font. See here. Or count the letters and be happy with less presision. Or, I guess, you could go with a hack and add a dummy first column..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111