hitechoutlaw Posted March 29, 2003 Posted March 29, 2003 i've seen this being used before but for some reason i cant get it to work. could some1 tell me what is wrong with my code: If OpenFileDialog1.ShowDialog = DialogResult.OK Then Dim Read As New System.IO.StreamReader(OpenFileDialog1.FileName) Dim Line() As Integer Dim i As Integer = 0 For Each i In Line Line(i) = Read.ReadLine Next 'Error occurs here TextBox1.Text = Line(0) TextBox2.Text = Line(1) TextBox3.Text = Line(2) End If thanks Quote
Guest mutant Posted March 29, 2003 Posted March 29, 2003 You did not specify how big the array is. I dont know but to me this code doesnt make any sense... Where did you find this? If you want only 3 integers do this: If OpenFileDialog1.ShowDialog = DialogResult.OK Then Dim Read As New System.IO.StreamReader(OpenFileDialog1.FileName) Dim Line(2) As Integer 'You need to say how big the array is 'if you are not going to redim it Dim i As Integer = 0 For Each i In Line Line(i) = Read.ReadLine Next TextBox1.Text = Line(0) TextBox2.Text = Line(1) TextBox3.Text = Line(2) End If Quote
hitechoutlaw Posted March 29, 2003 Author Posted March 29, 2003 i've seen bits and peices all around and tried to but them together. i think i know where u are talking about: Line(i) = Read.ReadLine right? now that is the place i dont know, i was trying to find an easy way to read a file cuz all the other examples i've seen are very long and compicated. i think this is my only error now. how can i make this read each line and put each line in the text boxes? can it be done with my current code or will i have to chage all of it? Quote
Guest mutant Posted March 29, 2003 Posted March 29, 2003 This will read 3 lines just like you wanted and put them in textboxes: Dim line(2) As String Dim x As Integer If OpenFileDialog1.ShowDialog = DialogResult.OK Then FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input) For x = 0 To 2 line(x) = LineInput(1) Next x TextBox1.Text = line(0) TextBox2.Text = line(1) TextBox3.Text = line(2) End If Quote
*Experts* Volte Posted March 29, 2003 *Experts* Posted March 29, 2003 You should declare 'Line' and 'i' as Strings, as that is what is stored and retrieved from text files. Other than that, it looks like it should work. Quote
*Experts* Volte Posted March 29, 2003 *Experts* Posted March 29, 2003 :eek: mutant! No! Never ever ever use FileOpen and related functions. They are provided for backward compatability (so that the program migration wizard doesn't have to convert the old VB6 way to the new .NET way), and should be avoided like the plague (at least, in my opinion). Awful coding practice, those functions. The System.IO namespace is what you use. Quote
Guest mutant Posted March 29, 2003 Posted March 29, 2003 Well, its there so it can be used, there is a choice, he can use that or a streamreader, his choice. Quote
*Experts* Volte Posted March 29, 2003 *Experts* Posted March 29, 2003 True, but if you want to practice good coding standards, then you use System.IO. You can use Goto and On Error and FileOpen and all the rest of the Vb6 compatability functions, but you shouldn't. They are not really supported by the .NET framework. As a general rule, I don't use anything that can't be used in both C# and VB.NET. Quote
hitechoutlaw Posted March 29, 2003 Author Posted March 29, 2003 well in my way it only reads the last line and it puts it in the first text box and in mutant's way it works great, i like my way cuz its easier for me to rember for later use and i will use it if i can find out why it is doing that, but if i cant i'll use mutant's way. thanks guys Quote
Guest mutant Posted March 29, 2003 Posted March 29, 2003 This is the same thing but with streamreader: Hope it helps. Dim Read As New System.IO.StreamReader(OpenFileDialog1.FileName) For x = 0 To 2 line(x) = Read.ReadLine Next x TextBox1.Text = line(0) TextBox2.Text = line(1) TextBox3.Text = line(2) Quote
fotini Posted September 12, 2003 Posted September 12, 2003 Thanks from me too! It's great! Quote la grecque
Ace Master Posted December 15, 2003 Posted December 15, 2003 I used that code for reading a 15 lines text file, but I want to make a split in each line to have a 2D array. Each line contains 4-6 elements separated by "vbtab" it's possible ? thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.