Visual Basic 2010 General DiscussionFor 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
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
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.
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.
'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)
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.
The Following User Says Thank You to Ian Ryder For This Useful Post: