By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Filling up a listview dynamically with file contents

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 news:O6**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 15 '05 #2

P: n/a
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 news:O6**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 15 '05 #3

P: n/a
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.
"Andy Bates" <An********@UltimateSoftwareSolutions.com> wrote in message news:ej****************@TK2MSFTNGP10.phx.gbl...
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 news:O6**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 15 '05 #4

P: n/a
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.
"Andy Bates" <An********@UltimateSoftwareSolutions.com> wrote in message news:ej****************@TK2MSFTNGP10.phx.gbl...
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 news:O6**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 15 '05 #5

P: n/a
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 news:%2****************@TK2MSFTNGP10.phx.gbl...
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.
"Andy Bates" <An********@UltimateSoftwareSolutions.com> wrote in message news:ej****************@TK2MSFTNGP10.phx.gbl...
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 news:O6**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 15 '05 #6

P: n/a
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 news:%2****************@TK2MSFTNGP10.phx.gbl...
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.
"Andy Bates" <An********@UltimateSoftwareSolutions.com> wrote in message news:ej****************@TK2MSFTNGP10.phx.gbl...
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 news:O6**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.