473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Filling up a listview dynamically with file contents

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
6 4604
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: (Pete Cresswell) | last post by:
TabControl on the right side of a form with two tabs: Tab #1 contains two subforms that show some dynamic query results. Tab #2 contains a ListView that gets dynamically populated as the user...
0
by: VM | last post by:
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();...
6
by: Vanessa | last post by:
With this program I can do one selection, but upon the second I get an error where ///////////////// is indicated. Please help. using System; using System.Drawing; using System.Collections;...
0
by: willow1480 | last post by:
I am developing a small little Service Control Application. I am using a listview control with checkboxes and getting the list of services I want to control from a text file. When you check a...
0
by: Zac Maclean | last post by:
I have a working version of this in VB, tryign to translate everything over to C#. In the C# version this is filling the listview, the first couple columns are ok, but the last few are going...
3
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
0
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this...
1
by: utkuozan | last post by:
I want to fill an existing ListView with colors and color codes. All color codes are held in an ArrayList as RGB. I have two columns in ListView. I want to fill the ListView with items as the color...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.