Dev Center > Windows Forms Forums > Windows Forms General > How to access textbox on parent form?

Answered How to access textbox on parent form?

  • Sunday, July 31, 2005 5:25 PM
     
     
    I'm trying to access a textbox on a parent form from the child form. Seems pretty simple, but I just can't find how to do it.

    This is how I'm opening the child form:

    Form fRoom = new RoomAllocator();
    fRoom.ShowDialog();

    How do I access the parent form's text box from the child form? Somebody please help!

Answers

  • Monday, August 01, 2005 9:38 AM
     
     Answered
    First, set the modifier of your textBox to internal (or public if the child form you are showing is from a different assembly) of your parent form to expose it, so other forms can have access to it by using the parent form's instance.

    Then, from your ShowDialog call, do it this way:


       Form fRoom = new RoomAllocator();
       fRoom.ShowDialog(this);



    To access the exposed textbox from RoomAllocator, all you have to do is, cast the Owner property value to the type of your parent form:


       TextBox parentTextBox = ((ParentForm)this.Owner).textBox1;
       parentTextBox.Text = "Textbox is updated from the child form";



    Best Regards,

    -chris

All Replies

  • Sunday, July 31, 2005 6:00 PM
     
     
    This is a common problem and is easily solved by searching for it: http://addressof.com/blog/articles/Form2Form.aspx
  • Sunday, July 31, 2005 7:04 PM
     
     
    Thanks, but that is a very long and complicated article! I just want a simple code snippet in C# that shows me how I can get my child form to populate the text box in the parent form! Anyone? I'm so tired of looking on the net for something that was so simple in Vb6.
  • Sunday, July 31, 2005 7:48 PM
     
     
    In short, what I'm asking is, how do I "set a property in the child form to the parent form" in C#?

    Somebody please This is driving me nuts!!Tongue Tied
  • Monday, August 01, 2005 9:38 AM
     
     Answered
    First, set the modifier of your textBox to internal (or public if the child form you are showing is from a different assembly) of your parent form to expose it, so other forms can have access to it by using the parent form's instance.

    Then, from your ShowDialog call, do it this way:


       Form fRoom = new RoomAllocator();
       fRoom.ShowDialog(this);



    To access the exposed textbox from RoomAllocator, all you have to do is, cast the Owner property value to the type of your parent form:


       TextBox parentTextBox = ((ParentForm)this.Owner).textBox1;
       parentTextBox.Text = "Textbox is updated from the child form";



    Best Regards,

    -chris
  • Monday, August 01, 2005 11:52 AM
     
     
    Why not try something allong the lines of:

    Create the following in RoomAllocator:

    A public refference variable. In VB it would look something like:

    Public ParentTextBoxRef as object

    Then change the parent like this: (again VB)

    dim fRoom as new RoomAllocator
    fRoom.ParentTextBoxRef = me.<the text box>

    You should then be able to access the textbox from the child.
  • Monday, August 01, 2005 7:41 PM
     
     
    thanks chris! it works!!!