• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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
0
NJJoeG8GT
Asked:
NJJoeG8GT
  • 4
  • 2
  • 2
  • +1
5 Solutions
 
Luis PérezSoftware Architect in .NetCommented:
You can create a void SetFilename function in child form passing one string parameter. Then, from parent form, you can call that function in ActiveMdiChild.

Hope that helps.
0
 
Minh Võ CôngCommented:
Form1:

publicpartialclass Form1 : Form
  {
    privateint _aSDJK = 27894;

    internalint asdfjkld
    {
      get { return _aSDJK; }
      set { _aSDJK = value; }
    }

    public Form1()
    {
      InitializeComponent();
      this.IsMdiContainer = true;
    }

    privatevoid Form1_Load(object sender, EventArgs e)
    {
      Form2 f = new Form2();
      f.MdiParent = this;
      f.Show();
    }
  }
Form2:

publicpartialclass Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
    }

    privatevoid Form2_Load(object sender, EventArgs e)
    {
      Form1 f = (Form1)this.MdiParent;
      MessageBox.Show(f.asdfjkld.ToString());
      f.asdfjkld++;
      MessageBox.Show(f.asdfjkld.ToString());
    }
  }
http://go4answers.webhost4life.com/Example/does-child-form-access-mdi-parents-136821.aspx
http://www.c-sharpcorner.com/forums/thread/124580/getting-variable-value-from-
mdiparent-form-to-child-form.aspx
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
NJJoeG8GTAuthor Commented:
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();

                 
               

             
            }
        }
0
 
Minh Võ CôngCommented:
FormChild has a richtextbox control? You can public the control

in FormChile
public RichTextBox  m_rtb;
in Parrent
  FormChild fc = new FormChild();
  fc.MdiParent = this;
  fc.Show();
  string str ="hello";
fc.m_rtb.Text = str;
str = fc.m_rtb.Text;

and so on ...
0
 
käµfm³d 👽Commented:
You can public the control
That's not a good suggestion. Making the RTB public means you are making every property the RTB exposes publicly available to any bit of code that has access to the RTB. Now code external to your form can do:

rtb.FindForm().Controls.Remove(rtb);

Open in new window


That seems like an awful amount of power to give up just for the sake of setting the RTBs text value. Expose a new property that grants access to just the RTB's Text property.
0
 
NJJoeG8GTAuthor Commented:
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);
                }
            }
        }
0
 
NJJoeG8GTAuthor Commented:
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();
0
 
NJJoeG8GTAuthor Commented:
it was the solution that i worked out.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now