Solved

C# passing a file name to a child window

Posted on 2013-06-17
9
376 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 74

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 74

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

21 Experts available now in Live!

Get 1:1 Help Now