473,698 Members | 2,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(listView1);

Thanks.
Nov 15 '05 #1
6 4646
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.A dd ("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.A dd ("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******** ******@TK2MSFTN GP10.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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(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.A dd ("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.A dd ("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******** ******@TK2MSFTN GP10.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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(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********@Ult imateSoftwareSo lutions.com> wrote in message news:ej******** ********@TK2MSF TNGP10.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.A dd ("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.A dd ("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******** ******@TK2MSFTN GP10.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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(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********@Ult imateSoftwareSo lutions.com> wrote in message news:ej******** ********@TK2MSF TNGP10.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.A dd ("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.A dd ("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******** ******@TK2MSFTN GP10.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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(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******** ********@TK2MSF TNGP10.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********@Ult imateSoftwareSo lutions.com> wrote in message news:ej******** ********@TK2MSF TNGP10.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.A dd ("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.A dd ("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******** ******@TK2MSFTN GP10.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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(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******** ********@TK2MSF TNGP10.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********@Ult imateSoftwareSo lutions.com> wrote in message news:ej******** ********@TK2MSF TNGP10.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.A dd ("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.A dd ("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******** ******@TK2MSFTN GP10.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.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("i tem1",0);
item1.Checked = true;
item1.SubItems. Add("1");
item1.SubItems. Add("2");
item1.SubItems. Add("3");
ListViewItem item2 = new ListViewItem("i tem2",1);
item2.SubItems. Add("4");
item2.SubItems. Add("5");
item2.SubItems. Add("6");
listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
listView1.Items .AddRange(new ListViewItem[]{item1,item2});
this.Controls.A dd(listView1);

Thanks.
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2430
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 navigates a TreeView on the left side of the form. The first time I load the ListView, if the tab containing it is not selected (i.e. the ListView is not visible) the screen draws the ListView's contents in the upper left portion of the form -...
0
378
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(); 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,
6
2531
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; using System.ComponentModel; using System.Windows.Forms; using System.Data;
0
2334
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 checkbox, it should either stop the service or start the service depending on each respective service's status. I want the listView to update the status for each service as they are checked. instead all I can seem to do is clear the listView and I...
0
4477
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 screwy. In VB, it is working perfectly. The reason for move is speed. Takes a full 10 - 15 seconds to run this on VB, <5 in C#. I adapted this from
3
9302
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, checkbox's and icons are displayed as they are meant to be. My problem is this, I am trying to achieve the same results as I get with CheckListBox(s), where I am able to select (Check-mark) various checkboxs items and able to alter the data. I do...
0
5419
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 board. I have a form that contains a ListView and a GroupBox control. The GroupBox control itself contains several TextBox Controls. I have Validating and Validated event handlers set for the GroupBox control, and a SelectedIndexChanged event...
1
1667
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 index as text and having a small thumbnail of the color as Image. Filling the text is quite easy. ( ColorIndexArray .ToString ( ) ) But how can I add the color, for example {123,23,41} as the image in front of the text? Is it possible to...
3
2149
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 them to the collection. Whenever I uncheck the checkbox, I want to add the items back into the listview from the collection, and remove them from the collection so I can do the process multiple times. The listview tag is being used as a key, so I...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.