Access Main Form Variables From Child

I looked through the archives and didn't really find an answer to my question.

In my Main form, I have a "global" array that holds some data.  I want to be able to access and make changes to that array from a child window.  I know that I can set up a "retrieval" method in the Main form to send the child form the array.  I do that with the following code:

SunSystem[] s = ((frmMain)this.MdiParent).GetArray();

I would rather just modify and access the array located on the frmMain in case I have multiple child forms open at once.  Is this possible

Thanks in advance,
Rob
rclint@NOSPAM.uwsp.edu
(remove NOSPAM to email me)


Answer this question

Access Main Form Variables From Child

  • davidc1110

    add a Constructor with argunment and data field into the Child Form. When you create and show the child form. use this Constructor and pass the value.


  • Nirupam Pratap Reddy

    Can you not use class with static properties   Then you can access the class by both in the same way.  

    James 

  • maxithron

    I looked into static a bit more and I think it is what I want but I am unsure how to implement it.  If I declare and fill my static class array in my Main Form.  How do I access that exact same array from my child windows

    This is what I have so far (minus unnecesary stuff):

    frmMain:
    public class frmMain : System.Windows.Forms.Form
    {
         static SunSystem[] s; 
         private void FillArray()
         {
    Random r = new Random();
    s = new SunSystem[r.Next(10,20)];

    for (int z = 0; z<s.Length;z++)
    {
    s[z] = new SunSystem(r,z);
    }
         }
         public void OpenChild(object sender, System.EventArgs e)
         {
    frmChild ss = new frmChild();
    ss.MdiParent=this;
    ss.Show();
         }
    }

  • BartonFinkle

    You are creating a new <b>reference</b> in the child form, but this reference points to the same object as the reference in the main form.

    Thats why it works!

    Remember that the code "private SunSystem[] s;" creates two things:  the "SunSystem[]" object in memory and a reference (or pointer) to that object called "s".

    The child form is creating a new reference, also called "s", and pointing it to the existing object created on the main form via the main form's reference to that object.

    Make sense

  • Greg Hurlman

    Thanks for the reply and I apologize for taking so long to reply.  I was on vacation.

    I was able to get the code converted to c#.  I figured it would be polite to reply in case some oens else wanted the c# code.  

    In the Main form I have the following code:
    private SunSystem[] s; 
    public SunSystem[] SunSystemArray
    {
     get 
        {
    return s;
         }
     set 
         {
    s = value;
         }
    }

    In the child form I have the following line:
    SunSystem[] s = ((frmMain)this.MdiParent).SunSystemArray;

    This succeeds in delivering the array to the child form and I can change the properties of the array.  Those properties are saved to the array on the main form.  I honestly am not sure why it works since it appears I am creating a new instance of the array on the child form but the changes made from the child form are saved to the main form.  

    Thanks again for all the help.

    RobC

  • ClaraOscura

    Here's one suggestion (there are other ways also)...

    Declare a public property on the main form that gets/sets the private Array.  Then all child windows can refer to This.MdiParent.ArrayProperty to access the array on the main form.

    I'm not sure how properties are declared in C# so here's a quick VB example you can translate:

    Public Class frmMain
         Inherits System.Windows.Forms.Form

         #Windows Designer Stuff#

         Private myS() As SunSystem

         Public Property SunSystemArray() As SunSystem()
              Get
                   Return myS
              End Get
              Set(ByVal value As SunSystem())
                   myS = value
              End Set
         End Property

        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             'Call Array Filling Code
        End Sub

    'Insert rest of form code

    End Class

    That will put a property on frmMain that allows access to the array.  The child forms would then access this property using code something like:

    CType(This.MdiParent, frmMain).SunSystemArray


    Another method might be to reverse this logic, putting the array property on the child forms and setting its value equal to the main form's array when the child is created.

    Hope that helps!

  • Access Main Form Variables From Child