c# - How to edit text in a text file using StreamWriter? -
i creating application displays contents of text file in listbox. have created edit form edit items. edits displayed in listbox finding difficult save them text file. suggestions how can this?
here code edit form:
public static arraylist switches = new arraylist(); public static frmswitches frmkeepswitches = null; public static string inputdatafile = "leckysafe.txt"; listbox listboxswitches; public frmeditswitch(listbox lstswitch) { initializecomponent(); listboxswitches = lstswitch; } public string newtext { { return txtserialno.text; } } private void btnreset_click(object sender, eventargs e) { txtserialno.text = ""; } private void frmeditswitch_load(object sender, eventargs e) { txtserialno.text = listboxswitches.selecteditem.tostring(); } private void btncancel_click(object sender, eventargs e) { frmswitches.frmkeepswitches.show(); this.close(); } private void btnsave_click(object sender, eventargs e) { listboxswitches.items[listboxswitches.selectedindex] = txtserialno.text; frmswitches.frmkeepswitches.show(); this.close(); } }
code on main form:
private void btnedit_click(object sender, eventargs e) { lstswitch_selectedindexchanged(null, null); frmeditswitch tempeditswitch = new frmeditswitch(lstswitch); tempeditswitch.show(); frmkeepswitches.hide(); //my line lstswitch.items[lstswitch.selectedindex] = tempeditswitch.newtext; }
using (streamwriter writer = new streamwriter(inputdatafile)) { foreach (var item in lstswitch.items) { writer.writeline(item.tostring()); } }
in general, show relevant code.
Comments
Post a Comment