Back to Blog
Once the data from the first pass of the Do While Loop has been dealt with in this way, the loop restarts and each row of the ListView is populated, one after the other, until the end of the file is reached.Īt this stage, the code shown above will produce this result: Again, because this ListView is using the Details view layout, each of these strings will be assigned to the next available column of the ListView. Next, two sub items are added to the ListViewItem – the contents of the second and third elements of the string array respectively. As you probably know, this means that in a Details View layout whatever is in that string will be displayed in the first column of the ListView. Then, the third line assigns the contents of the first element of the string array to the Text property of the ListViewItem. The second line adds the ListViewItem to the ListView on the form. The five lines of code in bold does the necessary work. In this step, we create an empty ListViewItem object and then populate it with the data from the string array: Next, we can see what is to be done with the data that’s held in that array: If you take the first line of the file shown at the top, then when the Split function is invoked for the first time, it will have three elements with contents as follows: In our example file, of course, there will be three such elements in the array. Finally, it stores any remnant following the final comma in another element of the array. What actually happens here is that the Split function trawls through the line of text and whenever it finds a comma it then plucks out the previous chunk of text and stores it in a separate element. In this case it’s the comma, which is Chr(44). This function needs to know which character (or characters) you want to use to split the line of data. In order to break the line of data into its three parts, you use the Split function. In this example, I’ve named that array strTemp. The idea is that as each line of the file is read via the StreamReader, you store that line in the string array. Once the loop has ended, the file has been completely read, so you can use the ‘End Using’ statement to release the StreamReader in order to conserve resources.īuild the String Array Using the Split Method The core code here is enclosed in a Using block. The “Do something here” will of course be items 3 to 6 in that list above,which I’ll describe shortly. The code will continue to loop until such time as there are no more lines to read from the file. The next line creates a Do While Loop and uses the Peek function of this class to check if the end of the file has been reached. This code creates the StreamReader object and passes in the name of the file that you want to read. Then make the following additions to the procedure:ĭim MyStream As New StreamReader("C:\Temp\KitchenItems.txt") I possibly should have named it something like LV_Items to make this clearer.)įirst, add an Imports statement for the System.IO namespace to the top of the file. (“lstItems” is of course the name that I assigned to the ListView. The code below puts this in a procedure named GetItems. In this example, we need to make sure that the ListView uses the Details View for the layout and also that there are three columns in the view. Something that catches a lot of people out is that the ListView won’t be able to display any data unless you first tell it how to do so. You’ll see how this works in the description that follows.Ĭreate the required columns in the ListView Finally, you add each of the remaining elements of data as sub items to that new ListViewItem, which is now part of the ListView.Then you add this ListViewItem to the ListView control.Next, you create a ListViewItem object and read the first element of data from the string array into this ListViewItem.As you build the array, you use the commas in the file to split the content into the three elements.Use a StreamReader to read the file into a string array.Create the required columns in the ListView.Here’s an outline of one way of achieving this result: Here’s the contents of an example text file:Īnd you’d like to have it displayed in a ListView like so: Depending on what that data is, a ListView might be a useful control to use for the display. Sometimes, you have data in a plain text file, you want to display it in a Windows Form.
0 Comments
Read More
Leave a Reply. |