Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Guest mutant
Posted

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

Posted

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?

Guest mutant
Posted

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

  • *Experts*
Posted

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.

  • *Experts*
Posted

: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.

Guest mutant
Posted
Well, its there so it can be used, there is a choice, he can use that or a streamreader, his choice.
  • *Experts*
Posted

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.

Posted

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

Guest mutant
Posted

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)

  • 5 months later...
  • 3 months later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...