StreamReader/Writer help

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
7 posts Page 1 of 1
Contributors
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

StreamReader/Writer help
GoodGuy17
Hello,
I need the code to do this:
I have a program, and you can add a title and description. When you click save, it makes a new text file in the program's folder with the name as the saved title, and it saves the description in the text file. When the program loads, it will load up the titles in a listbox and when you click the item in the listbox it will show the description in a textbox. There will be multiple text files.
Please please please help first correct answerer gets rep.
~GoodGuy17 :D
User avatar
domipoppe
Dedicated Member
Dedicated Member
Posts: 50
Joined: Sun Oct 04, 2009 1:05 pm

Re: StreamReader/Writer help
domipoppe
by my method it save 2 files so:
-------------------------------------------

My.Computer.FileSystem.WriteAllText("C:\titel.txt", title.text, False)
My.Computer.FileSystem.WriteAllText("C:\titel2.txt", description.text, False)

ListBox1 = The ListBox
TitelSetter = You see in the Code (TextBox Hiden)
RichTextBox1 = The Box there where the Description is showing...

Dim RT As New System.IO.StreamReader("C:\titel.txt")
On Error Resume Next
TitelSetter.Text = RT.ReadToEnd

Dim RTtwo As New System.IO.StreamReader("C:\titel.txt")
On Error Resume Next
ListBox1.Items.Add(RTtwo.ReadToEnd)

-> ListBox1 Selected Item

Dim RT As New System.IO.StreamReader("C:\titel2.txt")
On Error Resume Next

If ListBox1.SelectedItem = TitelSetter.text then
RichTextBox1.Text = RT.ReadToEnd
Else
MsgBox("Error")
End If

Or you take the .xml Method!

I hope that i can help you with this!
Visit my website!
http://www.e-software.de.tc
English version: http://tinyurl.com/esoftwarenglish
Please take original (german) version, the english has many bugs

Go to my Softwaresite or Gamesite :)
I am german, don't be angry for my not so good english.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: StreamReader/Writer help
GoodGuy17
On the load of the form, it loads all the titles into a listbox, the titles are the names of the text files in the folder. Then, when you click a title, it loads the description into a textbox. The code above did not look like it would work for this.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: StreamReader/Writer help
mandai
Code: Select all
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim files As String() = Directory.GetFiles(Environment.CurrentDirectory, "*.txt")

        For i As Integer = 0 To files.Length - 1
            ListBox1.Items.Add(Path.GetFileNameWithoutExtension(files(i)))
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex > -1 Then
            TextBox1.Text = File.ReadAllText(ListBox1.SelectedItem & ".txt")
        End If
    End Sub
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: StreamReader/Writer help
GoodGuy17
Mandai the only thing that doesnt work is it won't read the text in the description box. Here is my code:
Form with titles:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim files As String() = Directory.GetFiles(Environment.CurrentDirectory, "*.txt")

For i As Integer = 0 To files.Length - 1
ListBox1.Items.Add(Path.GetFileNameWithoutExtension(files(i)))
Next
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex > -1 Then
TextBox1.Text = ListBox1.SelectedItem.ToString
TextBox2.Text = File.ReadAllText(ListBox1.SelectedItem & ".txt")
End If
End Sub

Form that adds titles:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Writer As New System.IO.StreamWriter(TextBox1.Text & ".txt")
Writer.Write(TextBox2.Text)
End Sub
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: StreamReader/Writer help
CodenStuff
Hello Reboh,

Seems to work ok for me, it reads back the text into TextBox2 and also saved when I press the button. Change this code though.


Change this:
Code: Select all
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Writer As New System.IO.StreamWriter(TextBox1.Text & ".txt")
Writer.Write(TextBox2.Text)
End Sub
To this:
Code: Select all
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        IO.File.WriteAllText(Environment.CurrentDirectory & "\" & TextBox1.Text & ".txt", TextBox2.Text)
    End Sub

That worked ok for me cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: StreamReader/Writer help
GoodGuy17
Wait no Mandai's did work after I tweaked it a little. Thanks anyways CodenStuff :) Also, I made this to store my snippets, can I upload it or has someone already made something to keep snippets?
7 posts Page 1 of 1
Return to “Tutorial Requests”