Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles
Reading lines in textfiles Reading lines in textfiles
Reading lines in textfiles
Go Back  Xtreme Visual Basic Talk > > > Reading lines in textfiles


Reply
 
Thread Tools Display Modes
  #1  
Old 07-28-2007, 06:52 PM
Acin Acin is offline
Freshman
 
Join Date: Feb 2007
Posts: 25
Default Reading lines in textfiles


I am trying to make my program read a textfile and for each line in the textfile add the line to a combobox.

I have tried to do this:
Code:
Private Sub comboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.DropDown
        Dim fs As New FileStream("C:\test\readfile.txt", FileMode.Open, FileAccess.Read)
        Dim SR As New StreamReader(fs)
        Dim l
        comboBox1.Items.Clear()
        For Each l In SR.ReadToEnd
            comboBox1.Items.Add(l)
        Next
Reply With Quote
  #2  
Old 07-28-2007, 09:42 PM
AtmaWeapon's Avatar
AtmaWeaponReading lines in textfiles AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

ReadToEnd() returns the entire file as a single string. Your foreach loop is only iterating once, and adding the entire file as an item to the combo box. You probably want to use ReadLine() instead.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-28-2007, 10:11 PM
raxcodemax raxcodemax is offline
Newcomer
 
Join Date: Jul 2007
Posts: 3
Default

Just testing if I can reply.
I assume the above is a .NET program? Because I dont understand any of it. Im a old vb5 vb6 user. In vb5/6 the solution could be:

Private Sub insert_file(myfile as String, Optional clearfirst as Integer)
Dim txtline as String, exist as String
exist=Dir(myfile) ' Lets first check if file even exist
IF exist<>"" THEN
If clearfirst=1 then Combo1.Clear
Open myfile for input as #1
Do
Line Input #1, txtline
Combo1.Additem txtline
Loop Until EOF(1)
Close #1
ELSE
Msgbox "Error when trying to open the file:" + chr(13) + myfile, vbCritical, "File Error"
END IF
End Sub

You just send a

call insert_file("C:\Whatever\example.txt")
or from a file list control :
call insert_file(File1.List(File1.ListSelected))
and if you want combo list cleared as well :
call insert_file(File1.List(File1.ListSelected), 1)

- whenever you need lines added to that combobox. I am sure it could be expanded so other controls could be feeded with a file as well. Sadly I cant remember how. I often do the above in my small tools, so I hope you like my example (even if it dont apply to .net).

Not even sure that can apply to .net in any way. But vb5 stuff is easy I think and sadly thats my everyday tool. I might upgrade to .net 1 day but the trial I tested gave me headaches.

Last edited by raxcodemax; 07-28-2007 at 10:19 PM.
Reply With Quote
  #4  
Old 07-29-2007, 02:38 AM
Acin Acin is offline
Freshman
 
Join Date: Feb 2007
Posts: 25
Default

Quote:
Originally Posted by AtmaWeapon View Post
ReadToEnd() returns the entire file as a single string. Your foreach loop is only iterating once, and adding the entire file as an item to the combo box. You probably want to use ReadLine() instead.
Thanks for the reply.
I have tried to change sr.readtoend to sr.readline like this:
Code:
Private Sub comboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboBox1.DropDown
        Dim fs As New FileStream("C:\test\readfile.txt", FileMode.Open, FileAccess.Read)
        Dim SR As New StreamReader(fs)
        Dim l
        comboBox1.Items.Clear()
        For Each l In SR.ReadToEnd
            comboBox1.Items.Add(l)
        Next
But now it adds each letter in the first line as separate items, how can I make it add the two lines as separate items?

Last edited by Acin; 07-30-2007 at 01:58 AM.
Reply With Quote
  #5  
Old 07-29-2007, 09:57 AM
AtmaWeapon's Avatar
AtmaWeaponReading lines in textfiles AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

The code you just posted is identical to the first code you posted, so I can't really offer any concrete facts. However, the problem you are expressing sounds like you are using Read() instead of ReadLine.

Also raxcodemax it's generally a good idea to avoid posting in the .NET forums if you don't use .NET, even if you preface your post with "I don't know .NET but here's how I'd do it". Sometimes people don't read entire posts and you'll muddle them by posting code that doesn't work in .NET. There's some legacy VB forums if you scroll just a little farther down.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 07-30-2007, 01:52 AM
Acin Acin is offline
Freshman
 
Join Date: Feb 2007
Posts: 25
Default

Sorry, I have no idea why the code wasn't changed, I was 100% positive that I posted the modifyed code. This was what i tried:
Code:
Private Sub comboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboBox1.DropDown
        Dim fs As New FileStream("C:\test\readfile.txt", FileMode.Open, FileAccess.Read)
        Dim SR As New StreamReader(fs)
        Dim l
        comboBox1.Items.Clear()
        For Each l In SR.Readline
            comboBox1.Items.Add(l)
        Next
But I actually found an other way of doing this, I did this:
Code:
1:
            If SR.EndOfStream = False Then
                ComboBox1.Items.Add(SR.ReadLine)
                GoTo 1
But now I have an other problem with the same program, is it possible to prevent the program of adding duplicates to the combobox?

Last edited by Acin; 07-30-2007 at 01:58 AM.
Reply With Quote
  #7  
Old 07-30-2007, 07:56 AM
AtmaWeapon's Avatar
AtmaWeaponReading lines in textfiles AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Oh I see a problem there, and it can be solved by two little friends. You need to dig around in your language options and turn Option Strict On or place that line at the top of your code file. In addition, you have some bad habits that need to be forgotten ASAP.

Variable declarations such as: Dim l with no type information are dangerous and lead to silly errors that could easily be avoided.

Here's where your problem was:
For Each l in SR.Readline

l has no type information so it is basically Object. Now, what the For Each statement tells the compiler is "For each item in this array, perform these steps:". ReadLine doesn't return an array directly; it returns a single string that represents the line. The problem is, in some ways a string is an array of Char, so what is really happening is you get a For Each loop that says "For each character in this string do the following:".

Had Option Strict been turned on your code would not have compiled until you changed the declaration to:
Dim l as String and then your hint that something was wrong would be that the loop would execute once if the code even compiled at all.

Your new code is almost what you need, but there is something wrong with it. You should never ever use the GoTo statement without a good reason. It would be much clearer and the compiler is written to optimize for this functionally equivalent case:
Code:
While Not SR.EndOfStream
    ComboBox1.Items.Add(SR.ReadLine)
End While
In addition, you may want to consider adding () empty parenthesis to the ends of method calls; it makes the difference between properties and methods more dramatic.

To answer your new question, you might want to look into the Contains() method of the ComboBox's Items property; with that you can read a new value and then check to see if the ComboBox already contains it, then decide whether to add the new value or not.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 08-01-2007, 04:01 AM
Acin Acin is offline
Freshman
 
Join Date: Feb 2007
Posts: 25
Default

Thank you very much for all help, it has been very helpful.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles Reading lines in textfiles
Reading lines in textfiles
Reading lines in textfiles
 
Reading lines in textfiles
Reading lines in textfiles
 
-->