Page 1 of 1

StreamReader/Writer help

Posted: Sat Sep 04, 2010 1:49 am
by 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

Re: StreamReader/Writer help

Posted: Sat Sep 04, 2010 2:33 am
by 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!

Re: StreamReader/Writer help

Posted: Sat Sep 04, 2010 2:43 am
by 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.

Re: StreamReader/Writer help

Posted: Sat Sep 04, 2010 6:06 pm
by 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

Re: StreamReader/Writer help

Posted: Sat Sep 04, 2010 8:50 pm
by 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

Re: StreamReader/Writer help

Posted: Sun Sep 05, 2010 6:46 am
by 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;

Re: StreamReader/Writer help

Posted: Mon Sep 06, 2010 4:54 pm
by 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?