How to...

Posts 1–15 of 19 · Page 1 of 2
How to...
Just a quick question to the community, basically what i want to do is, Create a code in a richtextbox on vb, have a textbox1 for a title add text box1 title to a listbox whilst saving the code, then when you open the program again, when you click the list box index you get the code saved. I hope this makes sense and i hope anyone can help, i have done most i just really dont know how to look up a file from the previous form load.

Cheers in advance.
you can either save the contents to a .txt file and have it read the file upon opening or you have to create a database which is easy as well
with the database u set the listbox not by items but contents stored in database.
ill do up a tut if u want but im sure u can find some on youtube or something if u look
Quote Originally Posted by XiAtomikiX View Post
you can either save the contents to a .txt file and have it read the file upon opening or you have to create a database which is easy as well
with the database u set the listbox not by items but contents stored in database.
ill do up a tut if u want but im sure u can find some on youtube or something if u look
i have saved the contents to a text file, but on next open when you click the option you wish in the listbox i want to display the code again if that makes sense...
Quote Originally Posted by nay View Post
i have saved the contents to a text file, but on next open when you click the option you wish in the listbox i want to display the code again if that makes sense...
ok
1.show some source code (it helps better to understand what your doing)
2. what do u want to show when click on item in listbox - (is the item displaying contents to a label,textbox,etc)
Quote Originally Posted by XiAtomikiX View Post
ok
1.show some source code (it helps better to understand what your doing)
2. what do u want to show when click on item in listbox - (is the item displaying contents to a label,textbox,etc)
ok theres a list box with the default options in, to the left is a richtextbox where you can type in something and save (it saves the richtextbox to a text file and saves a title to the list box) so adds another option to the listbox. When you close the program and re open, i want to click the text box option and it will display the code i want (Hope this is more clear)
Quote Originally Posted by nay View Post
ok theres a list box with the default options in, to the left is a richtextbox where you can type in something and save (it saves the richtextbox to a text file and saves a title to the list box) so adds another option to the listbox. When you close the program and re open, i want to click the text box option and it will display the code i want (Hope this is more clear)
do already have it so when u save the text it saves the title to the listbox?
Quote Originally Posted by XiAtomikiX View Post
do already have it so when u save the text it saves the title to the listbox?
yes, i also have it so it saves the code, all i want to do is when you open again and click the new option in the list box it will show the code.
Quote Originally Posted by nay View Post
yes, i also have it so it saves the code, all i want to do is when you open again and click the new option in the list box it will show the code.
i think understand what your doing

if your saving each text as a seperate Textfile than all you need to do is make it so when you click on the Selected item it displays the contents of said txt file to
but like i said i would have to see how you saved the title to listbox in order to show u how to list the contents of the file
i can add a item to list box and set it so when i click on
if ur using just as a file reader you can do this

Code:
    fileReader = My.Computer.FileSystem.ReadAllText("Text.txt")
        If ListBox1.SelectedItem = "Test" Then
            RichTextBox1.Text = fileReader
        End If
if you have multiple titles in 1 text file(meaning your only using 1 txt file then you can use StreamReader
this is an example of StreamReader
Code:
dim reader as New StreamReader("Text.txt") 
 If ListBox1.SelectedItem = "Test" Then
            RichTextBox1.Text = reader.ReadLine
        End If
Quote Originally Posted by XiAtomikiX View Post
i think understand what your doing

if your saving each text as a seperate Textfile than all you need to do is make it so when you click on the Selected item it displays the contents of said txt file to
but like i said i would have to see how you saved the title to listbox in order to show u how to list the contents of the file
i can add a item to list box and set it so when i click on
if ur using just as a file reader you can do this

Code:
    fileReader = My.Computer.FileSystem.ReadAllText("Text.txt")
        If ListBox1.SelectedItem = "Test" Then
            RichTextBox1.Text = fileReader
        End If
if you have multiple titles in 1 text file(meaning your only using 1 txt file then you can use StreamReader
this is an example of StreamReader
Code:
dim reader as New StreamReader("Text.txt") 
 If ListBox1.SelectedItem = "Test" Then
            RichTextBox1.Text = reader.ReadLine
        End If
ok i understand this but you will never know what name someone is saving to the list box (i could save my code as lololol) above you have put (ListBox1.SelectedItem = "Test") any ideas on how i could check the saved list of my list box and then open the code ? (if that makes sense lol)
Quote Originally Posted by nay View Post
ok i understand this but you will never know what name someone is saving to the list box (i could save my code as lololol) above you have put (ListBox1.SelectedItem = "Test") any ideas on how i could check the saved list of my list box and then open the code ? (if that makes sense lol)
thats why i said pm me your source lol

if your using Stream Reader then you would do
Code:
dim reader as New StreamReader(listbox1.SelectedItem + ".txt") ' will grab the user clicked event on listbox1 and open that txt file
 RichTextBox1.Text = reader.ReadLine ' reads txt file by 1 line. you would have to either do a For Each loop or a While loop to get each line or do ReadToEnd
I have made this sample code 3 different ways and its works fine for me but i dont know how your saving the title and text file without that i can only give u tips not a whole example. so if u would be kind to just pm me the source i can give u more examples.
Quote Originally Posted by XiAtomikiX View Post
thats why i said pm me your source lol

if your using Stream Reader then you would do
Code:
dim reader as New StreamReader(listbox1.SelectedItem + ".txt") ' will grab the user clicked event on listbox1 and open that txt file
 RichTextBox1.Text = reader.ReadLine ' reads txt file by 1 line. you would have to either do a For Each loop or a While loop to get each line or do ReadToEnd
I have made this sample code 3 different ways and its works fine for me but i dont know how your saving the title and text file without that i can only give u tips not a whole example. so if u would be kind to just pm me the source i can give u more examples.
well i cant pm you as i dont have enough posts so here it is
Code:
Imports System****
Public Class Form2
    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'ListBox1.Items(ListBox1.SelectedIndex)
    End Sub

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Clear()
        ListBox1.Items.AddRange(IO.File.ReadAllLines("ListB.txt"))
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        IO.File.WriteAllLines("ListB.txt", (From p As String In ListBox1.Items Select p).ToArray)
        IO.File.WriteAllLines(TextBox1.Text & "/" & "code.txt", From n As String In RichTextBox1.Text Select n)
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

        Dim Test As New Long
        'Dim test As New String = IO.File.ReadAllLines(" // needs doing
        IO.File.ReadAllLines("ListB.txt")
        'IO.File.ReadLines
        'If ListBox1.SelectedIndex = 
        Select Case ListBox1.SelectedIndex
            Case 0
                RichTextBox1.Text = "Code here"
            Case 1
                RichTextBox1.Text = "Code1 here"
            Case 2
                RichTextBox1.Text = "Code2 here"
        End Select
    End Sub
End Class
Its a little messy as i have not finished yet but anyway.
Quote Originally Posted by nay View Post
well i cant pm you as i dont have enough posts so here it is
Code:
Imports System****
Public Class Form2
    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'ListBox1.Items(ListBox1.SelectedIndex)
    End Sub

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Clear()
        ListBox1.Items.AddRange(IO.File.ReadAllLines("ListB.txt"))
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        IO.File.WriteAllLines("ListB.txt", (From p As String In ListBox1.Items Select p).ToArray)
        IO.File.WriteAllLines(TextBox1.Text & "/" & "code.txt", From n As String In RichTextBox1.Text Select n)
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

        Dim Test As New Long
        'Dim test As New String = IO.File.ReadAllLines(" // needs doing
        IO.File.ReadAllLines("ListB.txt")
        'IO.File.ReadLines
        'If ListBox1.SelectedIndex = 
        Select Case ListBox1.SelectedIndex
            Case 0
                RichTextBox1.Text = "Code here"
            Case 1
                RichTextBox1.Text = "Code1 here"
            Case 2
                RichTextBox1.Text = "Code2 here"
        End Select
    End Sub
End Class
Its a little messy as i have not finished yet but anyway.
well i think with what i see your better off using xml to store the contents of the listbox (easier to grab in my book)
then you can use streamreader and streamwriter to write and read the text file

with the source below create a file named "list.xml"
Code:
<?xml version="1.0" encoding="utf-8"?>
<main> 
</main>
put that in it and save then
you can use the source below and it will make the whole thing work
Code:
Imports System****

Public Class Form1



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xdoc As New Xml.XmlDocument
        xdoc.Load("list.xml")
        Dim titleNode As Xml.XmlNode = xdoc.Item("main")
        Dim NewElement As Xml.XmlElement
        NewElement = xdoc.CreateElement("title") ' creates new element <title>valuehere</title>
        NewElement.InnerText = TextBox1.Text
        titleNode.AppendChild(NewElement)
        xdoc.Save("list.xml") ' Saves the xml file with the content added
        ListBox1.Items.Clear()
        UpdateList()
        Dim path As String = TextBox1.Text + ".txt"
        Dim writer As New StreamWriter(path)
        Dim text As String = RichTextBox1.Text
        writer.Write(text)
        writer.Close()
    End Sub
    Public Sub UpdateList()
        ListBox1.Items.Clear()
        Dim xdoc As New Xml.XmlDocument
        xdoc.Load("list.xml")
        Dim titleNode As Xml.XmlNode = xdoc.Item("main")
        Dim element As Xml.XmlElement


        For Each element In titleNode
            ListBox1.Items.Add(element.InnerText)
        Next
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Clear()
        UpdateList()

    End Sub

    Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
        Dim loadpath As String = ListBox1.SelectedItem + ".txt"
        Dim reader As New StreamReader(loadpath)
        Do While reader.Peek() >= 0
            RichTextBox1.Text = reader.ReadToEnd()
        Loop
        reader.Close()
    End Sub

End Class
the way it works is save the textbox.text as the title of the .txt file you save then when you dblclick the item in listbox it opens the txt file inside of a richtextbox

i think this is what u were trying to accomplish.
Database is just over reacting for something so simple.

An outside file, settings, w/e will work.
Quote Originally Posted by 'Bruno View Post
Database is just over reacting for something so simple.

An outside file, settings, w/e will work.
thatswhy i said a txt file will work if its a simple thing your trying to do. if its anymore then that its to much
you can even save to xml file which is better in my book.
So, you want a load/save the contents of your listbox using a text file?
I don't understand exactly
Posts 1–15 of 19 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us