Link to home
Start Free TrialLog in
Avatar of Joseph Longo
Joseph LongoFlag for United States of America

asked on

C# passing a file name to a child window

I need to open the file that i selected in the parent window.. in the child window.
my plan is to pass the child form the filename and then open it and display it in the child form.
How do i pass the filename to the child window?

i have attached source.
Form1.cs
FormChild.cs
ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joseph Longo

ASKER

ok.  thanks for all replies.  I am still struggling with this.
i cant even get the parent window to show files contents.

how do i get the string text  contents into the parent window?
at least i can start here...

see this code:



private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofdOpenFile = new OpenFileDialog();
            ofdOpenFile.Title = "Select a File To Open";
            ofdOpenFile.FileName = "";
            ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (ofdOpenFile.ShowDialog() == DialogResult.OK)
            {
                string file = ofdOpenFile.FileName;
                string text = File.ReadAllText(file);

               
                //RichTextBox rtb = GetRichTextBox();
               
             
                    FormChild fc = new FormChild();
                    fc.MdiParent = this;
                    fc.Show();
                    Application.DoEvents();

                 
               

             
            }
        }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
when i use the fc.Text command it uses "hello" in the title bar of the new child window.
how do i get that value into the body of the child window?

private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofdOpenFile = new OpenFileDialog();
            ofdOpenFile.Title = "Select a File To Open";
            ofdOpenFile.FileName = "";
            ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (ofdOpenFile.ShowDialog() == DialogResult.OK)
            {
                RichTextBox rtb = GetRichTextBox();

                if (rtb == null)
                {
                   
                    FormChild fc = new FormChild();
                    fc.MdiParent = this;
                    fc.Show();
                   
                    string str = "hello";
                    fc.Text = str;
                    //str = fc.Text;


                   
                 
                    Application.DoEvents();
                                     
                }

                if (rtb != null)
                {
                    rtb.LoadFile(ofdOpenFile.FileName, RichTextBoxStreamType.RichText);
                }
            }
        }
this was solved like this.
in the child form add an argument for passed filename and load into rechtextbox:

public FormChild(string fileName)
        {
            InitializeComponent();
            richTextBox1.LoadFile(fileName, RichTextBoxStreamType.PlainText);
           
        }
in the parent form pass the name:
                FormChild fc = new FormChild(ofdOpenFile.FileName);
                fc.MdiParent = this;
                fc.Show();
it was the solution that i worked out.