Adding subitems in a ListView

G

Guest

If item is a ListViewItem and str is a string, why do the following two lines
not have the same effect ?

item.SubItems.Add(new ListViewItem.ListViewSubItem()).Text =
str;
item.SubItems.Add(str);

In the example code that follows, using the first line (in the ListView
constructor) causes the later redisplay of values (to some specified number
of decimal places) in the ListView not to work. Using the second line causes
the redisplay to work as expected.

I am using C#.NET 2003, Standard Edition.

Any ideas ?
Thanks, Andrew.

// example code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Modify
{
/// <summary>
/// Summary description for Form1.
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label1;
private double[] X = {0.13579, 0.02468}; // values to display

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
ConstructColumns();
BuildList(); // causes Modify() not to work correctly.
//BuildList1(); // ok
// why are BuildList() and BuildList1() not equivalent ?

}

/// <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()
{
this.listView1 = new System.Windows.Forms.ListView();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// listView1
//
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(56, 32);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(144, 97);
this.listView1.TabIndex = 1;
this.listView1.View = System.Windows.Forms.View.Details;
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(144, 152);
this.numericUpDown1.Maximum = new System.Decimal(new int[] {
12,
0,
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(48, 20);
this.numericUpDown1.TabIndex = 2;
this.numericUpDown1.Value = new System.Decimal(new int[] {
2,
0,
0,
0});
this.numericUpDown1.ValueChanged += new
System.EventHandler(this.numericUpDown1_ValueChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 152);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 23);
this.label1.TabIndex = 3;
this.label1.Text = "Decimal Places";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(240, 197);
this.Controls.Add(this.label1);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);

}
#endregion

private void ConstructColumns()
{
// sets up two columns in ListView1
HorizontalAlignment centre = HorizontalAlignment.Center;
listView1.Columns.Add("A",70,centre); // why is first column not centred ?
listView1.Columns.Add("B",70,centre);
}

public void BuildList()
{
string fmt = "{0:F" + (int) numericUpDown1.Value + "}";
for (int i = 0; i <= 1; i++) // two rows
{
// build row
ListViewItem item = listView1.Items.Add(i.ToString()); // first column
item.SubItems.Add(new ListViewItem.ListViewSubItem()).Text =
String.Format(fmt,X); // second column

}
}

public void BuildList1()
{
string fmt = "{0:F" + (int) numericUpDown1.Value + "}";
for (int i = 0; i <= 1; i++) // two rows
{
// build row
ListViewItem item = listView1.Items.Add(i.ToString()); // first column
item.SubItems.Add(String.Format(fmt,X)); // second column
}
}


public void Modify()
{
// change # decimal places used in column 2.
// does not remove rows or columns : just changes subitem.Text for second
column
string fmt = "{0:F" + (int) numericUpDown1.Value + "}";
foreach (ListViewItem item in listView1.Items)
{
int i = listView1.Items.IndexOf(item); // row index starting from zero
ListViewItem.ListViewSubItemCollection list = item.SubItems;
foreach (ListViewItem.ListViewSubItem subitem in list)
{
if (list.IndexOf(subitem) > 0) // column index starting from zero
{
subitem.Text = String.Format(fmt,X);
}
}
}
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
{
Modify(); // does not work if BuildList() used in constructor of Form1;
works if BuildList1() used.
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top