c# - code structure for streamWrite -
is there better way me streamwrite without needing repeat code every button press? below example working on 1 button it's own thing , writes accordingly vowels , other same thing except writes accordingly no alpha characters:
private void btnvowels_click(object sender, eventargs e) { string wholetext = ""; string copytext = richtextbox1.text; if (system.io.file.exists(second_file) == true) { system.io.streamwriter objwriter; objwriter = new system.io.streamwriter(second_file); string vowels = "aaeeiioouu"; copytext = new string(copytext.where(c => !vowels.contains(c)).toarray()); wholetext = richtextbox1.text + copytext; objwriter.write(wholetext); richtextbox2.text = wholetext; objwriter.close(); } else { messagebox.show("no file named " + second_file); } } private void btnalpha_click(object sender, eventargs e) { string wholetext = ""; string copytext = richtextbox1.text; if (system.io.file.exists(second_file) == true) { system.io.streamwriter objwriter; objwriter = new system.io.streamwriter(second_file); string nonalpha = @"[^a-za-z ]+"; string addspace = ""; copytext = regex.replace(copytext, nonalpha, addspace); objwriter.write(wholetext); richtextbox2.text = wholetext; objwriter.close(); } else { messagebox.show("no file named " + second_file); } }
why not doing way?
private void write(string file, string text) { if (file.exists(file)) { using (streamwriter objwriter = new streamwriter(file)) { objwriter.write(text); } } else { messagebox.show("no file named " + file); } } private void btnalpha_click(object sender, eventargs e) { string wholetext = ""; string copytext = richtextbox1.text; string nonalpha = @"[^a-za-z ]+"; string addspace = ""; copytext = regex.replace(copytext, nonalpha, addspace); wholetext = richtextbox1.text + copytext; write(second_file, wholetext); // same second button richtextbox2.text = wholetext; }
Comments
Post a Comment