Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello I am trying to save a listbox as a txt file.

 

Language: visual basic .net

 

 

i used the code below, and it actually saved the file, but the contents that where in the listbox were not saved with the file. if someone could modify my code below, or even better, make a small example :D, i would greatly appriciate it!

 

code i used:

SaveFileDialog1.Filter = "Text Documents (*.txt)|*.txt|All Files|*.*"

SaveFileDialog1.ShowDialog()

If SaveFileDialog1.FileName <> "" Then

FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)

PrintLine(1, listbox1.Text.Length)

FileClose(1)

End If

 

thank you

  • *Experts*
Posted

One way would be to do this:

Dim x As Integer
Dim writer As New IO.StreamWriter("E:\somefile.txt") 
'create a new streamwriter to write to the file
For x = 0 To l.Items.Count - 1 'make sure to go through every item
   writer.WriteLine(l.Items(x)) 'write the item to the file
Next
writer.Flush() 'make sure everything is dumped into the file
writer.Close() 'close the stream

  • *Experts*
Posted

How would you expect us to help you if we don't know what's wrong?

 

You can copy the full text of an error by right clicking it in the Task List and choosing Copy.

Posted
i want them to be able to pick where it will be saved at.. so that code is wrong. please just make a form with a save button and listbox, and make it save all the contents of that listbox as a txt file.. please only reply now if you are going to attach the file
Posted

LOL, this post is making me laugh.

 

All you need to do is post your errors. I think it probley takes more effect to moan than do this simple task.

 

By the way there being pushy and just dam impolite, you figure.

 

Andy

Code today gone tomorrow!
  • *Experts*
Posted
Simply put a SaveFileDialog onto your form, or create a new one in code. Then before saving anything show it using the ShowDialog method. After that, when createing a new StreamWriter pass in the SaveFileDialog1.FileName value which will contain the path which was selected.
Posted

:(

 

i have no clue how to do it, so assuming the save file dialogs name is savefiledialog1, how would the code look if the code i have now is the following:

 

Dim x As Integer

Dim writer As New IO.StreamWriter("c:\somefile.txt")

'create a new streamwriter to write to the file

For x = 0 To idlist.Items.Count - 1 'make sure to go through every item

writer.WriteLine(idlist.Items(x)) 'write the item to the file

Next

writer.Flush() 'make sure everything is dumped into the file

writer.Close() 'close the stream

 

thank you, i really appriciate the help :)

  • *Experts*
Posted

If SaveFileDialog1.ShowDialog = DialogResult.OK Then 'make sure a path was selected
  Dim x As Integer
  'subsitutue the path with the path from the dialog
  Dim writer As New IO.StreamWriter(SaveFileDialog1.FileName)
  'create a new streamwriter to write to the file
  For x = 0 To idlist.Items.Count - 1 'make sure to go through every item
  writer.WriteLine(idlist.Items(x)) 'write the item to the file
  Next
  writer.Flush() 'make sure everything is dumped into the file
  writer.Close() 'close the stream
End If

:)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...