Click to See Complete Forum and Search --> : Access controls from another form (mdi)
tonyf
May 9th, 2003, 04:20 AM
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?
coolbiz
May 9th, 2003, 06:58 AM
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
tonyf
May 9th, 2003, 06:59 AM
am just getting to grips with vb.net so not really sure how to do that.
can you give me any pointers?
coolbiz
May 9th, 2003, 10:53 AM
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:
' 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
tonyf
May 9th, 2003, 10:54 AM
many thanks.
pepesmith
June 26th, 2003, 08:03 PM
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...
coolbiz
June 27th, 2003, 06:27 AM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.