soadlink Posted October 13, 2003 Posted October 13, 2003 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 Quote
*Experts* mutant Posted October 13, 2003 *Experts* Posted October 13, 2003 You want to write all the items that are in the ListBox to the file? Quote
soadlink Posted October 14, 2003 Author Posted October 14, 2003 yes, take everything in the listbox, and save it as a txt file. Quote
*Experts* mutant Posted October 14, 2003 *Experts* Posted October 14, 2003 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 Quote
soadlink Posted October 16, 2003 Author Posted October 16, 2003 cant seem to get it to work .. if possible could u attach the source to all the projects files? or give a different way? Quote
*Experts* mutant Posted October 16, 2003 *Experts* Posted October 16, 2003 What is the problem? Quote
soadlink Posted October 16, 2003 Author Posted October 16, 2003 it gave me a buncha errors, could you just make a little form with a save button, listbox, etc and upload it for me to dl? :( i just cant get it. Quote
*Experts* mutant Posted October 16, 2003 *Experts* Posted October 16, 2003 It wouldnt make a difference, I would just put the same code in it as I showed you. What are the errors you are getting? Quote
soadlink Posted October 16, 2003 Author Posted October 16, 2003 lets put it this way, too many errors for my lazy self to post =\ Quote
*Experts* Volte Posted October 16, 2003 *Experts* Posted October 16, 2003 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. Quote
soadlink Posted October 16, 2003 Author Posted October 16, 2003 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 Quote
Administrators PlausiblyDamp Posted October 16, 2003 Administrators Posted October 16, 2003 Just guessing but demanding help like that isn't going to get many favourable responses. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
soadlink Posted October 16, 2003 Author Posted October 16, 2003 then dont respond, im sorry if im pushy its just so simple for you people but hard for me, so if you arent gonna post good help then dont post. Quote
a_jam_sandwich Posted October 16, 2003 Posted October 16, 2003 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 Quote Code today gone tomorrow!
soadlink Posted October 17, 2003 Author Posted October 17, 2003 Ok i got it to work, but i want it use the savefiledialog thingy so they can CHOOSE where the file will be saved. how could that be pulled off? thanks ;) Quote
*Experts* mutant Posted October 17, 2003 *Experts* Posted October 17, 2003 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. Quote
soadlink Posted October 17, 2003 Author Posted October 17, 2003 :( 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 :) Quote
*Experts* mutant Posted October 17, 2003 *Experts* Posted October 17, 2003 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 :) Quote
soadlink Posted October 17, 2003 Author Posted October 17, 2003 (edited) edited post: woo hoo! works! and i got it to filter out so it only saves as a .txt. thank you :) Edited October 17, 2003 by soadlink Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.