c# - Windows Forms text editor search function -
i have 2 forms, 1 text editor, , second search form.
in form1 have defined getrichtextbox()
function. works there, how retrieve form2 (and other stuff)?
my form1:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace texteditor { public partial class form1 : form { public form1() { initializecomponent(); } private richtextbox getrichtextbox() { richtextbox rtb = null; tabpage tp = tabcontrol1.selectedtab; if (tp != null) { rtb = tp.controls[0] richtextbox; } return rtb; } private void newtoolstripmenuitem_click(object sender, eventargs e) { tabpage tp = new tabpage("doc"); richtextbox rtb = new richtextbox(); rtb.dock = dockstyle.fill; tp.controls.add(rtb); tabcontrol1.tabpages.add(tp); } private void cuttoolstripmenuitem_click(object sender, eventargs e) { getrichtextbox().cut(); } private void copytoolstripmenuitem_click(object sender, eventargs e) { getrichtextbox().copy(); } private void pastetoolstripmenuitem_click(object sender, eventargs e) { getrichtextbox().paste(); } private void undotoolstripmenuitem_click(object sender, eventargs e) { getrichtextbox().undo(); } private void redotoolstripmenuitem_click(object sender, eventargs e) { getrichtextbox().redo(); } private void opentoolstripmenuitem_click(object sender, eventargs e) { openfiledialog openfd = new openfiledialog(); string chosen_file = ""; openfd.initialdirectory = "c:"; openfd.title = "open text file"; openfd.filename = ""; openfd.filter = "text files|*.txt|word documents|*.doc"; if (openfd.showdialog() != dialogresult.cancel) { chosen_file = openfd.filename; tabpage tab = new tabpage() { text = system.io.path.getfilename(chosen_file) }; tabcontrol1.tabpages.add(tab); tabcontrol1.selectedtab = tab; richtextbox box = new richtextbox { parent = tab, dock = dockstyle.fill }; box.loadfile(chosen_file, richtextboxstreamtype.plaintext); } } private void savetoolstripmenuitem_click(object sender, eventargs e) { savefiledialog savefd = new savefiledialog(); string saved_file = ""; savefd.initialdirectory = "c:"; savefd.title = "save text file"; savefd.filename = ""; savefd.filter = "text files|*.txt|word documents|*.doc"; if (savefd.showdialog() != dialogresult.cancel) { saved_file = savefd.filename; getrichtextbox().savefile(saved_file, richtextboxstreamtype.plaintext); messagebox.show("your file has been saved!", "success", messageboxbuttons.ok, messageboxicon.information); } } private void exittoolstripmenuitem_click(object sender, eventargs e) { if (messagebox.show("are sure?", "exit", messageboxbuttons.okcancel) == dialogresult.ok) { application.exit(); } } private void closetabtoolstripmenuitem_click(object sender, eventargs e) { tabpage active_tab = tabcontrol1.selectedtab; tabcontrol1.tabpages.remove(active_tab); } private void searchtoolstripmenuitem_click(object sender, eventargs e) { var search = new form2(); search.showdialog(); } } }
and ofcourse, form2:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace texteditor { public partial class form2 : form { public form2() { initializecomponent(); } private void button1_click(object sender, eventargs e) { int index = 0; string temp = richtextbox1.text; richtextbox1.text = ""; richtextbox1.text = temp; while (index < richtextbox1.text.lastindexof(textbox1.text)) { richtextbox1.find(textbox1.text, index, richtextbox1.textlength, richtextboxfinds.none); richtextbox1.selectionbackcolor = color.orange; index = richtextbox1.text.indexof(textbox1.text, index) + 1; } } } }
Comments
Post a Comment