• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Writing to file problem...

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Hi All.
Trying to save listbox.items(text) to a file and I keep getting a error - unable to process the file as another process is using it. So what am I doing wrong???

Code:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim file_name As String = "c:" & txtFileName.Text & ".txt"
        System.IO.File.Create(file_name)
        If System.IO.File.Exists(file_name) = True Then
            Dim objWriter As New System.IO.StreamWriter(file_name)
            Dim int As Object
            int = ListBox2.Items.ToString
            objWriter.Write(int)
            objWriter.Write("This is a test")
            objWriter.Close()
        End If
    End Sub
Thanks...
 

Bob Langlade

Well-known member
Joined
Sep 20, 2005
Messages
115
Location
Saint-Pierre & Miquelon Islands
Programming Experience
3-5
Code:
        Dim file_name As String = "c:\Test.txt"
        Try
            Dim objWriter As New System.IO.StreamWriter(file_name)
            For Each item In ListBox2.Items
                objWriter.WriteLine(item.ToString)
            Next
            objWriter.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Bob do you have time for another question...
...If I send that file_name to the listbox on a different form (I can do this part) when I select the index how can I pull up the file in the texbox1 (just below the listbox)? Opps I might have found it... it seems to work, will have to try it with more than 1 file in the listbox later...

Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            Dim item As String
            item = ListBox1.SelectedItem
            If System.IO.File.Exists(item.ToString) = True Then
                Dim objReader As New System.IO.StreamReader(item.ToString)
                TextBox1.Text = objReader.ReadToEnd
            End If
        Catch
        End Try
    End Sub
Thanks again Bob...
 
Last edited:

Bob Langlade

Well-known member
Joined
Sep 20, 2005
Messages
115
Location
Saint-Pierre & Miquelon Islands
Programming Experience
3-5
Yes it's working, but you can convert your item to string only once.

Code:
        TextBox1.Text = "" 'To Clear the textbox if the file doesn't exist
        Dim Item As String = ListBox1.SelectedItem.ToString
        If System.IO.File.Exists(Item) = True Then
            Dim objReader As New System.IO.StreamReader(Item)
            TextBox1.Text = objReader.ReadToEnd
        End If
 

FluidOne

Active member
Joined
Nov 22, 2010
Messages
26
Programming Experience
3-5
Dont create a string variable everytime SelectedIndexChange occurs, huge waste.

try
If System.IO.File.Exists(listbox1.SelectedItem)=true the
...
 

FluidOne

Active member
Joined
Nov 22, 2010
Messages
26
Programming Experience
3-5
Dont create a string variable everytime SelectedIndexChange occurs, huge waste.

try
If System.IO.File.Exists(listbox1.SelectedItem)=true the

You may not want to read an entire file into a textbox everytime the listbox SelectedIndexChange event occurs!

...
Attach a read file button.
Attach a popup menu to the listbox.
But do change the code. Unless of course your listbox only has one item in it.
 
Top Bottom