Solved

C# passing a file name to a child window

Posted on 2013-06-17
9
377 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
  • 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now