vb.net - writing content of listbox to a txt file and then opening it -
i trying save contents of 1 listbox txt file , open , printing second listbox. have done of this, actual file not txt file. can u show me how make file txt file using code? if not possible can u show me code can?
private sub loadbutton_click(byval sender system.object, byval e system.eventargs) handles loadbutton.click if openfd.showdialog() = dialogresult.ok dim lines = file.readalllines(openfd.filename) listbox2.items.clear() listbox2.items.addrange(lines) end if end sub private sub savebutton_click(byval sender system.object, byval e system.eventargs) handles savebutton.click if savefd.showdialog() = dialogresult.ok using writer = new streamwriter(savefd.filename) each o object in listbox1.items writer.writeline(o) next end using end if end sub
you can use file.writealltext
method (along string builder). opinion, it's cleaner code.
private sub savebutton_click(byval sender system.object, byval e system.eventargs) handles savebutton.click if savefd.showdialog() = dialogresult.ok dim sb new system.text.stringbuilder() each o object in listbox1.items sb.appendline(o) next system.io.file.writealltext("c:\mypath\output.txt", sb.tostring()) end if end sub
Comments
Post a Comment