CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2003
    Location
    Nottingham
    Posts
    25

    Unhappy Access controls from another form (mdi)

    I have a main form that has a Statusbar control on it. I am then opening several child forms within this main form.

    How do i access the Statusbar control from within these child forms? any ideas?

  2. #2
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    You will need to pass the reference of your main form to the child forms. Then a child form can refer any controls on the main form through that reference.

    -Cool Bizs

  3. #3
    Join Date
    May 2003
    Location
    Nottingham
    Posts
    25
    am just getting to grips with vb.net so not really sure how to do that.

    can you give me any pointers?

  4. #4
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    There are few ways to do this. Here is one.

    Lets say your main FORM class name is FMain and your child FORM class is called FChild. Then consider the following:
    Code:
    ' your main FORM
    public class FMain
    ...
      ' when you add a control the declaration should look like this
      friend withevents txtUsername as textbox
    ...
    
      ' show the child
      private sub showchild()
        dim myChild as new FChild()
        myChild.ParentForm = me
        ....
      end sub
    ...
    end class
    
    ' your child FORM
    public class FChild
    ...
      ' var to hold the parent
      public ParentForm as FMain = Nothing
    ...
      ' access the textbox in main form
      private sub DummyProc()
        if (not m_MainForm is nothing) then m_MainForm.txtUsername.Text = "From CHILD class."
      end sub
    ...
    end class
    Again, you have so many ways depending on how you instantiate the child form. The basic idea is to have a reference to the parent object so that you can access any exposed members (declared with either PUBLIC or FRIEND).

    //EDIT: variable name in child form was corrected.

    -Cool Bizs
    Last edited by coolbiz; May 9th, 2003 at 10:57 AM.

  5. #5
    Join Date
    May 2003
    Location
    Nottingham
    Posts
    25
    many thanks.

  6. #6
    Join Date
    Oct 2002
    Location
    in front of computer
    Posts
    28

    follow up question...()

    my apology for using your thread sir tonyf

    Sir CoolBiz, How about using the MDIParent as one of the property the child form instead of making an instance ParentForm in the child form? Is it possible?

    Thanks...
    "ninja ray makapatay ninjo..mga limbarok mong dako" --
    in english...-->
    " If you wish to make an improved product, you must already be engaged in making an inferior one. "

  7. #7
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    You're probably right. If MDI child form exposes a variable MDIParent, that should also point to the parent form. Try it out and post the relsult

    -Cool Bizs

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

Featured