c# - Write to text files -
totally new this. depending on event button click need write appropriate text file. however, input data writes same text file. how specify appropriate text file writes , saved.
the coding, identical apart name of text file, i'm using is:
private void btnitemadd_click(object sender, eventargs e) { string sitem; string snumber; if (rdodrinks.checked == false && rdoconfectionary.checked == false) { //message remind user select category messagebox.show("please select category"); txtitem.focus(); } { { if ((rdodrinks.checked == true) && (txtitem.text != "") && (txtitemnumber.text == "")) //message remind user enter number messagebox.show("please input number"); txtitemnumber.focus(); if ((txtitem.text != "") && (txtitemnumber.text != "")) { //add item end of list lstitems.items.add(string.format("{0, -15} {1, -20}", txtitem.text, txtitemnumber.text)); txtcount.text = lstitems.items.count.tostring(); //set focus text box txtitem.focus(); streamwriter sw = file.appendtext("drinks.txt"); { sitem = txtitem.text; sw.writeline(sitem); snumber = txtitemnumber.text; sw.writeline(snumber); } messagebox.show("details have been saved"); txtitem.clear(); txtitemnumber.clear(); sw.close(); } else if ((rdoconfectionary.checked == true) && (txtitem.text != "") && (txtitemnumber.text == "")) //message remind user enter number messagebox.show("please input number"); txtitemnumber.focus(); if ((txtitem.text != "") && (txtitemnumber.text != "")) { //add item end of list lstitems.items.add(string.format("{0, -15} {1, -20}", txtitem.text, txtitemnumber.text)); txtcount.text = lstitems.items.count.tostring(); //set focus text box txtitem.focus(); streamwriter sw = file.appendtext("confectionary.txt"); { sitem = txtitem.text; sw.writeline(sitem); snumber = txtitemnumber.text; sw.writeline(snumber); } messagebox.show("details have been saved"); txtitem.clear(); txtitemnumber.clear(); sw.close(); } } } }
you have several issues:
you missing else after first
if
.the code below can never end being
true
.
the 2 if
statement collide (the txtitemnumber.text
comparison problem). hence never end writing drinks.txt:
if ((rdodrinks.checked == true) && (txtitem.text != "") && (txtitemnumber.text == "")) ... if ((txtitem.text != "") && (txtitemnumber.text != ""))
Comments
Post a Comment