Passing text to a text box from an MDI Parent to Child`

Hi All,

This is probably a stupid question but I am trying to rewrite a program that is in VB.net to C# - both 2003.

In VB I displayed a child form with the following code:

Dim child As New RichText
child.MdiParent = Me
child.txtLoadCommand.Text = strBuffer ' *** I can't do this bit in C#
child.Show()

In C# I'm using the following code:

ChildRichText newMDIChild = new ChildRichText();

newMDIChild.MdiParent = this; // Set the Parent Form of the Child window.

newMDIChild.Show(); // Display the new form.

I need help with getting the parent to set the text in the child form's text box to a string because I use the string to work out what the child is to do. I can't access the control the way I did in VB because of a protection level error. Can someone help please

Kind regards,

Justine.




Answer this question

Passing text to a text box from an MDI Parent to Child`

  • Trelly

    Give your your mdi form class a property:


    public class ChildRichText : Form
    {
    private string _myString;

    public string MyString
    {
    get
    {
    return _myString;
    }
    set
    {
    _myString = value;
    }
    }
    }




    Now you can get and set this property:


    ChildRichText newMDIChild = new ChildRichText();
    newMDIChild.MyString = "This is my string!";
    newMDIChild.MdiParent = this; // Set the Parent Form of the Child window.
    newMDIChild.Show(); // Display the new form.



    I hope this is clear anough, this example uses a string but you can use any type you want.


  • SKasic

    Thanks PJ,

    I was using the text box from my VB 6 days as a place to put information on what I wanted the child to do and your first example made the text box useless. But your second example explained your point very well.

    Thanks for your time.

    Justine.



  • No.1 Of Mew

    Everyone has to start somewhere with learning
    For all details about Properties you can take a look at this msdn page.

    An property is an facade over a private field of a class or struct (Form). It can provide read and/or write access to a private field. It can contain check's so that you can't set a wrong value for example. The 'get' is called whenever a property is readed and 'set' is called whenever a property is writen.

    When you want to make the Text property of an Textbox on you form public, you do this with a property:


    public class MyForm : Form
    {
    private TextBox _myTextbox;

    public string TextBoxText
    {
    get
    {
    // Return the value.
    return _myTextbox.Text;
    }
    set
    {
    // Set the value of the Text property
    // of _myTextbox to the given value.
    _myTextbox.Text = value;
    }
    }
    }




  • IloveThis

    Hi PJ,

    Thanks for your reply - it works and I don't need invisible text boxes!

    Just let me walk me through it to see if I understand how it works (I'm only a beginner at this) and I can't figure it all out.

    By making the function MyString public, any code under main can run the method consisting of get and set.

    In the parent, the now public method is seen and appears in the little drop down box. Setting MyString equal to some text in the parent sets it in the child but which is run - get or set and why

    Thanks heaps for your help so far and I've learned to mark your post as an answer but I really want to understand how it works.

    Once again thanks - and looking forward to any further guidance on how it works,

    Justine.



  • Passing text to a text box from an MDI Parent to Child`