openfiledialog - Method for "adding" new data to current open file C# -
i writing program have 2 listboxes same data 1 listbox items update student name , total score , other student name , each individual judge score next student name. going far stuck... have 2 methods save() , saveas() save() automatically writes data "formdata.bin" , saveas() lets user enter own file name.
is possible re-write save() method when click save saves current data file open in ms word when typing in document , click save add new typed data current file. here save() method wrote.
public void saveentry() { int itemscount = math.min(lstbxstudents.items.count, lstbxstudentscore.items.count); savefiledialog1.initialdirectory = application.startuppath; savefiledialog1.filename = "formdata.bin"; { try { using (filestream fs = new filestream(savefiledialog1.filename, filemode.create)) using (binarywriter save = new binarywriter(fs)) { save.write(cmbbxage.text); save.write(cmbbxbelt.text); save.write(cmbbxcategorie.text); save.write(cmbbxgender.text); save.write(cmbbxgup.text); save.write(txtjudge1.text); save.write(txtjudge2.text); save.write(txtjudge3.text); save.write(txtjudge4.text); save.write(txtjudge5.text); save.write(txtoperator.text); save.write(txtpos1.text); save.write(txtpos2.text); save.write(txtpos3.text); save.write(txtpos4.text); save.write(txtpos5.text); (int = 0; < itemscount; i++) { save.write(lstbxstudents.items[i].tostring()); save.write(lstbxstudentscore.items[i].tostring()); } save.close(); fs.close(); } } catch (exception error) { messagebox.show(error.message, "ctsd forms"); } } }
thank in advance
here image of form
if understood correctly trying achieve append text rather overwrite it. take upon streamwriter class allows specify if want append data or not when constructing instance. allows create/append file directly, without need of stream
.
also, consider using proper names variables:
save
action, not object. writer
better.
by using write in way showed in code, end texts appended no separators. should consider using writeline functions.
[edit upon clarifications]
saveentry
should take parameter specify if save done saveas
or save
. first save should saveas
. filename should saved in context (your form class, better have "view model")
private string _currentfilename; public void saveentry(bool saveas) { if (saveas || string.isnullorempty(_currentfilename)) { savefiledialog1.initialdirectory = application.startuppath; savefiledialog1.filename = "formdata.bin"; var result = savefiledialog1.showdialog(); // todo: handle user cancellation _currentfilename = savefiledialog1.filename; } using (var writer = new streamwriter(_currentfilename)) { // todo: stuff writer } }
Comments
Post a Comment