Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2012, 07:51 AM
Registered User
 
Join Date: Sep 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default VB 2010 text file to listbox

hi, i want to open a text file into a listbox, this i have managed. however i need to take out the first line of the text file and put that into a label once i have opened the text file. any help please
 
Old September 3rd, 2012, 09:19 AM
Authorized User
 
Join Date: Mar 2012
Posts: 59
Thanks: 2
Thanked 4 Times in 4 Posts
Default

Hi,

Please can you post the first few lines of your file so that I can see what you are trying to achieve. - (You cannot load files in this forum so please wrap the first few lines of the file in quote tags to keep the formatting) - In essence, this should be easily achievable by searching for the first Carriage Return, which then gets placed in the label and then the rest goes in the Listbox.

Cheers

Ian.
 
Old September 3rd, 2012, 09:24 AM
Registered User
 
Join Date: Sep 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

VB10
01/06/2010,140
02/06/2010,255
03/06/2010,252
04/06/2010,314
07/06/2010,28
08/06/2010,309
09/06/2010,69
10/06/2010,222
11/06/2010,81
14/06/2010,236
15/06/2010,171
16/06/2010,217
17/06/2010,285
18/06/2010,82
21/06/2010,203


That is some of the text file... the "VB10" is what i want to extract and make that the label.
 
Old September 3rd, 2012, 10:19 AM
Authorized User
 
Join Date: Mar 2012
Posts: 59
Thanks: 2
Thanked 4 Times in 4 Posts
Default

Hi,

Here is an example of how you can achieve what you are trying to do:-

1) Add a Label and a Listbox to a form.
2) Copy the code below to the form load event ensuring you replace the test.txt filename with your own file location and name.

Code:
    
'read the file
    Dim fileContents As String
    fileContents = My.Computer.FileSystem.ReadAllText("d:\temp\test.txt")
    'strip out the first line of the file using indexof to find the first CR/LF and then apply to the label
    Label1.Text = fileContents.Substring(0, fileContents.IndexOf(vbCrLf))
    'strip out the first line of the file, compensating for the space needed for CR/LF and then apply to the listbox
    fileContents = fileContents.Substring(fileContents.IndexOf(vbCrLf) + vbCrLf.Length, fileContents.Length - fileContents.IndexOf(vbCrLf) - vbCrLf.Length)
    ListBox1.Items.Add(fileContents)
I notice that the subsequent lines in the file have multiple fields? So a question would be:- Is a list box the right control for what you want to do depending on what you are trying to show?

Hope this helps.

Kind regards,

Ian
The Following User Says Thank You to Ian Ryder For This Useful Post:
botes82 (September 3rd, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Vb.Net 2003 : How to Open and Read an XML File to TEXT File. Lennie VS.NET 2002/2003 0 February 12th, 2010 05:18 PM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Save PDF file as text file in VB.Net kvenkatu Classic ASP Basics 0 April 7th, 2006 01:09 PM
save a PDF file as text file through VB. NET kvenkatu VB.NET 0 April 6th, 2006 12:15 PM
Save my ListBox data into text file kaifong78 VB.NET 2002/2003 Basics 0 September 14th, 2004 11:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.