Results 1 to 2 of 2

Thread: [2.0] MDI child call parent variable/object

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    24

    [2.0] MDI child call parent variable/object

    Can MDI child call back parent variable or any object?
    e.g.
    - frmParent
    -Public String conn = "abc.mdb";

    - frmChild
    - Public String test = "demo";

    - frmChildTest
    - how can we frmParent.conn ?
    - and can we call frmChild.test , too ?

    the issue i would like to call parent variable,
    It would like to create the database connection in parent ,
    so each child only need to call parent connection, and dont need to recreate on each form

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] MDI child call parent variable/object

    Every MDI child has a reference to its parent in its MdiParent property, which YOU would have explicitly set when you created it. The reference is type Form so you will need to cast it as the appropriate type to access members of that type, e.g.
    Code:
    MessageBox.Show(((frmParent)this.MdiParent).conn)
    Having said that, you should be using application settings for this type of thing in C# 2.0. You go to the Settings tab of the project properties and create a setting, named ConnectionString for example, of type string with Application scope. Now you can access that value ANYWHERE in your app, e.g.
    Code:
    MessageBox.Show(Properties.Settings.Default.ConnectionString);
    There is even a specialised type of setting for connection strings, which you can select from the drop-down list in the Type column instead of string. It will then allow you to build your connection string visually in the Value column instead of having to type it character by character. You can also bind properties of components created in the designer to application settings, so you could create your connection in the designer for each child form and bind its ConnectionString property to this setting, so it gets loaded automatically every time.
    Attached Images Attached Images  
    Last edited by jmcilhinney; May 30th, 2006 at 05:57 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width