How to...

Posts 16–19 of 19 · Page 2 of 2
Quote Originally Posted by XiAtomikiX View Post
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.
cheers pal.
Cheers for the long sentence
Quote Originally Posted by XiAtomikiX View Post
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.
also will i need to call the xml on form load?
Quote Originally Posted by nay View Post
also will i need to call the xml on form load?
Code:
ListBox1.Items.Clear()
        UpdateList() ' this thread contains the code to load xml into listbox
then the update list is called
Code:
    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
all of this was in the source code i just posted.
Posts 16–19 of 19 · Page 2 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us