Filling up a listview dynamically with file contents

V

VM

How can I fill up a listview with text file contents?
My listview has two columns and the first column fills up with a while loop:

while (myString != null)
{
myString = sr.Readline();
listView1.Items.Add (myString);
}

Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
I'm using a counter to tell it which line to fill up and adding to the second column is directly related to the first item (eg. I can't add a subitem on the 2nd column unlee I have added one in the 1st column).

Also, the MSDN sample doesn't work correctly. In the following code from MSDN, if I copy/paste it'll work perfectly. But if I comment the first line so it uses the listView I created in the designer (instead of creating a new one), it doesn't have the same effect.

// ListView listView1 = new ListView(); /* Commented so it uses existing listView1 */
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("item1",0);
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Items.AddRange(new ListViewItem[]{item1,item2});
this.Controls.Add(listView1);

Thanks.
 
A

Andy Bates

Hi -

If I understand you correctly then the following will do the job (providing that your list view has defined the two columns to drop the text into):

while (myString != null)
{
myString = sr.Readline();
ListViewItem item = listView1.Items.Add (myString);
item.SubItems.Add ("Second Column");
}

It would probably be better to re-org the code to be:

while (null != (myString = sr.ReadLine ())
{
ListViewItem item = listView1.Items.Add (myString);
item.SubItems.Add ("Second Column Text");
}

As obviously the null will be processed by the loop in your code!

HTH


- Andy
"VM" <Vm> wrote in message How can I fill up a listview with text file contents?
My listview has two columns and the first column fills up with a while loop:

while (myString != null)
{
myString = sr.Readline();
listView1.Items.Add (myString);
}

Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
I'm using a counter to tell it which line to fill up and adding to the second column is directly related to the first item (eg. I can't add a subitem on the 2nd column unlee I have added one in the 1st column).

Also, the MSDN sample doesn't work correctly. In the following code from MSDN, if I copy/paste it'll work perfectly. But if I comment the first line so it uses the listView I created in the designer (instead of creating a new one), it doesn't have the same effect.

// ListView listView1 = new ListView(); /* Commented so it uses existing listView1 */
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("item1",0);
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Items.AddRange(new ListViewItem[]{item1,item2});
this.Controls.Add(listView1);

Thanks.
 
V

VM

Thanks for your reply.
And regarding the MSDN example, why doesn't it work if I use my existing listview? If I create a simple form with a listView1 and copy that code into a button, it will create an instance of the listview with the 1st line (plus the listview I already had). But if I comment that line so it uses the existing listview with the same name, it shows a different behavior.
I would've thought it would throw an error since there are two objects withe the same name.

Thanks.
Hi -

If I understand you correctly then the following will do the job (providing that your list view has defined the two columns to drop the text into):

while (myString != null)
{
myString = sr.Readline();
ListViewItem item = listView1.Items.Add (myString);
item.SubItems.Add ("Second Column");
}

It would probably be better to re-org the code to be:

while (null != (myString = sr.ReadLine ())
{
ListViewItem item = listView1.Items.Add (myString);
item.SubItems.Add ("Second Column Text");
}

As obviously the null will be processed by the loop in your code!

HTH


- Andy
"VM" <Vm> wrote in message How can I fill up a listview with text file contents?
My listview has two columns and the first column fills up with a while loop:

while (myString != null)
{
myString = sr.Readline();
listView1.Items.Add (myString);
}

Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
I'm using a counter to tell it which line to fill up and adding to the second column is directly related to the first item (eg. I can't add a subitem on the 2nd column unlee I have added one in the 1st column).

Also, the MSDN sample doesn't work correctly. In the following code from MSDN, if I copy/paste it'll work perfectly. But if I comment the first line so it uses the listView I created in the designer (instead of creating a new one), it doesn't have the same effect.

// ListView listView1 = new ListView(); /* Commented so it uses existing listView1 */
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("item1",0);
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Items.AddRange(new ListViewItem[]{item1,item2});
this.Controls.Add(listView1);

Thanks.
 
A

Andy Bates

Hi -

The problem with the example is that by the time you add the items to the listview it has been laid out in preparation for display. Adding the columns/items in the Load method (presumably) to the member listview means that the columns are not laid out correctly.

If you change the width of the columns to a fixed value (basically change -2 to 50), then the columns will appear correctly. You also don't require the last line adding the control to the forms list as it will have already been added.

The original code works because the listview is constructed and initialised correctly prior to display.

Regards

- Andy
"VM" <Vm> wrote in message Thanks for your reply.
And regarding the MSDN example, why doesn't it work if I use my existing listview? If I create a simple form with a listView1 and copy that code into a button, it will create an instance of the listview with the 1st line (plus the listview I already had). But if I comment that line so it uses the existing listview with the same name, it shows a different behavior.
I would've thought it would throw an error since there are two objects withe the same name.

Thanks.
Hi -

If I understand you correctly then the following will do the job (providing that your list view has defined the two columns to drop the text into):

while (myString != null)
{
myString = sr.Readline();
ListViewItem item = listView1.Items.Add (myString);
item.SubItems.Add ("Second Column");
}

It would probably be better to re-org the code to be:

while (null != (myString = sr.ReadLine ())
{
ListViewItem item = listView1.Items.Add (myString);
item.SubItems.Add ("Second Column Text");
}

As obviously the null will be processed by the loop in your code!

HTH


- Andy
"VM" <Vm> wrote in message How can I fill up a listview with text file contents?
My listview has two columns and the first column fills up with a while loop:

while (myString != null)
{
myString = sr.Readline();
listView1.Items.Add (myString);
}

Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
I'm using a counter to tell it which line to fill up and adding to the second column is directly related to the first item (eg. I can't add a subitem on the 2nd column unlee I have added one in the 1st column).

Also, the MSDN sample doesn't work correctly. In the following code from MSDN, if I copy/paste it'll work perfectly. But if I comment the first line so it uses the listView I created in the designer (instead of creating a new one), it doesn't have the same effect.

// ListView listView1 = new ListView(); /* Commented so it uses existing listView1 */
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("item1",0);
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Items.AddRange(new ListViewItem[]{item1,item2});
this.Controls.Add(listView1);

Thanks.
 

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