Solved

C# passing a file name to a child window

Posted on 2013-06-17
9
383 Views
Last Modified: 2013-06-19
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
Comment
Question by:NJJoeG8GT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 150 total points
ID: 39253405
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 160 total points
ID: 39253448
0
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 190 total points
ID: 39253511
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:NJJoeG8GT
ID: 39255114
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
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 190 total points
ID: 39255132
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 160 total points
ID: 39255145
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
 

Author Comment

by:NJJoeG8GT
ID: 39257827
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
 

Author Comment

by:NJJoeG8GT
ID: 39259299
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
 

Author Closing Comment

by:NJJoeG8GT
ID: 39259337
it was the solution that i worked out.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question