Home All Groups Group Topic Archive Search About

help readin text file

Author
24 Nov 2006 6:09 PM
Marc
So i have a text file where each line represents a button name.

What the code I need to read this file  and creatre a new button from
each line..


so far I just have code to read the whole file


     Dim TextFileStream As System.IO.TextReade
        TextFileStream = System.IO.File.OpenText("C:\MyTextFile.txt")
         Dim MyFileContents As String = TextFileStream.ReadToEnd

Author
24 Nov 2006 6:42 PM
Newbie Coder
Here's your solution:

' Import
Imports System.io

' Add two buttons & add this code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim sr As New StreamReader("C:\ButtonNames.txt")
        Dim strTemp As String
        Dim intTop As Integer = 50
        Do While sr.Peek > -1
            strTemp = sr.ReadLine
            Dim btn As New Button
            btn.Name = "btn" & strTemp
            btn.Text = strTemp
            btn.Left = 10
            btn.Height = 24
            btn.Top = intTop
            Me.Controls.Add(btn)
            Me.Invalidate()
            intTop += 50
        Loop
        sr.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub

I created a textfile like so

One
Two
Three
Four

Have added the project to this post too

I hope this helps,

Newbie Coder



Show quoteHide quote
"Marc" <marc_cro***@hotmail.com> wrote in message
news:1164391781.764860.138030@j72g2000cwa.googlegroups.com...
> So i have a text file where each line represents a button name.
>
> What the code I need to read this file  and creatre a new button from
> each line..
>
>
> so far I just have code to read the whole file
>
>
>      Dim TextFileStream As System.IO.TextReade
>         TextFileStream = System.IO.File.OpenText("C:\MyTextFile.txt")
>          Dim MyFileContents As String = TextFileStream.ReadToEnd
>

[attached file: Button Names.zip]
Author
24 Nov 2006 11:22 PM
Herfried K. Wagner [MVP]
"Marc" <marc_cro***@hotmail.com> schrieb:
> So i have a text file where each line represents a button name.
>
> What the code I need to read this file  and creatre a new button from
> each line..

Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
25 Nov 2006 12:54 AM
Branco Medeiros
Marc wrote:
<snip>
>      Dim TextFileStream As System.IO.TextReade
>         TextFileStream = System.IO.File.OpenText("C:\MyTextFile.txt")
>          Dim MyFileContents As String = TextFileStream.ReadToEnd

Cool!

You learn a new thing everyday. Having seen System.IO.File.OpenText, I
wondered if there was something like 'ReadToEnd'. And so it has:

  Dim Text As String = System.IO.File.ReadAllText(FileName)

or, if you prefer it already split into lines:

  Dim Lines() As String = System.IO.File.ReadAllLines(FileName)


Regards,

Branco.