c# - RichTextBox contents are not updated while saving -
i have list box control in form contains paths of files of particular type folder. on item double click adding page dynamically tab control , loading contents of file object of rich text box. want edit contents , save again . when open saved file edited contents not saved has earlier contents there while loading file rich text box.how update rich text box object text , save.
private void lsterrorlist_mousedoubleclick(object sender, mouseeventargs e) { arraylist errortype = new arraylist(); richtextbox myrich = new richtextbox(); string[] list; tabpage selectedtab; if (lsterrorlist.items.count > 0) { string error = lsterrorlist.selecteditem.tostring(); int result = error.lastindexof('\\'); string filename = error.substring(result + 1, error.length - (result + 1)); list = error.split(new char[] { '\t' }); int pagecount; tabpage tp = new tabpage(); pagecount = this.tabcontrol1.tabpages.count; bool found = false; foreach (tabpage tab in tabcontrol1.tabpages) { if (filename.equals(tab.name)) { tabcontrol1.selectedtab = tab; found = true; break; } } if (!found) { tabcontrol1.tabpages.add(filename, filename); tabcontrol1.selectedtab = tabcontrol1.tabpages[tabcontrol1.tabpages.count - 1]; int = tabcontrol1.tabpages.count; myrich.height = this.tabcontrol1.height - 30; myrich.width = this.tabcontrol1.width - 10; myrich.anchor = (anchorstyles.left | anchorstyles.right | anchorstyles.top | anchorstyles.bottom); tabcontrol1.tabpages[tabcontrol1.tabpages.count - 1].controls.add(myrich); string path = list[7]; objreader = new system.io.streamreader(path); myrich.text = objreader.readtoend(); objreader.close(); } int val = 0; string val1 = list[3]; string replacement = regex.replace(val1, @"\t|\n|\r|[a-za-z]", ""); val = convert.toint32(replacement); foreach (control ct in tabcontrol1.selectedtab.controls) { if (ct richtextbox) { richtextbox x = (richtextbox)ct; x.select(val, wordtofind.length); x.selectionbackcolor = color.wheat; x.focus(); break; } } } } private void mnuvalidate_click(object sender, eventargs e) { myrich.refresh(); myrich.update(); foreach (tabpage page in tabcontrol1.tabpages) { string saved_file = ""; savefd.title = "save file"; savefd.filename = chosenfilename; savefd.filter = "text file|*.txt|html file|*.html|xhtml file|*.xhtml|xml file|*.xml"; saved_file = savefd.filename; foreach (control ct in tabcontrol1.selectedtab.controls) { if (ct richtextbox) { int x = tabcontrol1.selectedtab.controls.indexof(ct); messagebox.show(x.tostring()); ((richtextbox)page.controls[x]).savefile(saved_file,richtextboxstreamtype.richtext); } } this.tabcontrol1.tabpages.remove(page); } lsterrorlist.items.clear(); if (filepathlist.count == 0) { messagebox.show("no input files found,please upload files , validate again", "warning", messageboxbuttons.ok, messageboxicon.information); } else { if (html_qc_multiplefiles.errors.checkeditemlist.count == 0) { messagebox.show("please select error type , validate again", "information", messageboxbuttons.ok, messageboxicon.information); } else { if (singlefile == true) { validate(); } else { bool errorfound = false; string[] words; foreach (string file in filepathlist) { int lineno, index; objreader = new system.io.streamreader(file); myrich.clear(); myrich.height = this.tabcontrol1.height - 30; myrich.width = this.tabcontrol1.width - 10; myrich.anchor = (anchorstyles.left | anchorstyles.right | anchorstyles.top | anchorstyles.bottom); myrich.text = objreader.readtoend(); chosenfilename = file; validate(); objreader.close(); } } } } }
i think problem could code asks filename save , iterates through set of controls saving each same file. if have 2 rich text boxes efforts of saving first overwritten second.
other things out for:
- is there exception during save?
- is file being saved same name first?
- why save dialog filter *.rtf, *.txt , *.html when
savefile
method saves rtf? - path manipulations should done via
system.io.path
class. - consider introducing methods/functions particular activities don't end such large, wall-of-code methods.
Comments
Post a Comment