richtextbox - Opening a file from a parent form into a child form's text box C# -


i'm new @ c# , i'm having difficulties project i'm doing. have mdi program 2 forms. need open file richtextbox in child form (form2) menustrip in parent form (form1). how richtextbox child form using method parent form? don't know i'm doing wrong. appreciated. following code on parent form. thanks!

   private void opentoolstripmenuitem_click(object sender, eventargs e)     {         openfiledialog openfiledialog1 = new openfiledialog();         ofd.filter = "text files|*.txt";         openfiledialog1.title = "";         if (this.openfiledialog1.showdialog() == dialogresult.ok)         {             form2 f2 = new form2();             f2.mdiparent = this;             f2.show();             streamreader sr = new streamreader(openfiledialog1.filename);             text = sr.readtoend();             sr.close();           }     } 

there lot of ways this.

one way add file name constructor of child form. main form contain

form2 f2 = new form2(openfiledialog1.filename); f2.mdiparent = this; f2.show(); 

and in child form's constructor, define this:

public void form2(string filename); {     streamreader sr = new streamreader(filename);     this.richtextbox1.text = sr.readtoend();     sr.close(); } 

the above sensible option if child form cannot legally shown without associated file name, because force caller provide file name when constructing it.

if child form has fileless mode (e.g. when creating new document) can use different method. provide public property file name.

parent:

form2 f2 = new form2() f2.openfile(openfiledialog1.filename); f2.mdiparent = this; f2.show(); 

child:

public void openfile(string filename); {     streamreader sr = new streamreader(filename);     this.richtextbox1.text = sr.readtoend();     sr.close(); } 

finally, if you'd rather have file i/o logic in mdi class, can expose textbox:

parent:

form2 f2 = new form2() streamreader sr = new streamreader(openfiledialog1.filename); f2.documenttext = sr.readtoend(); sr.close(); f2.mdiparent = this; f2.show(); 

child:

public string documenttext {     set     {         this.richtextbox1.text = value;     } } 

one advantage of passing file name instead of text can set window's title bar show file name.

child:

public void form2(string filename); {     streamreader sr = new streamreader(filename);     this.richtextbox1.text = sr.readtoend();     this.text = string.format("notepad -- {0}", filename); //title     sr.close(); } 

or

public void openfile(string filename); {     streamreader sr = new streamreader(filename);     this.richtextbox1.text = sr.readtoend();     sr.close();     this.text = string.format("notepad -- {0}", filename); //title } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -