accessing / setting properties

A

alex

Hi,
how can I access / set properties of controls in other forms.
i.e. mdi parent of mdi child
mdi child of mdi child
mdi child of mdi parent

Thanks
Alex
 
F

Fergus Cooney

Hi Alex,

You need to have a reference to the other Form.

Each child has an MdiParent property which is nice and straightforward.
The parent Form has an MdiChildren collection which you'll have to rummage
in to find a specific child Form.

Regards,
Fergus
 
A

alex

Fergus,
hmm... I have a status bar in the MdiParent and from the child calling
Me.MdiParent.stBar.Text = "some text" gives me the error

stBar is not a member of System.Windows.Forms.Form.

What am I doing wrong ?

Thanks
Alex
 
A

Armin Zingler

alex said:
Fergus,
hmm... I have a status bar in the MdiParent and from the child
calling Me.MdiParent.stBar.Text = "some text" gives me the error

stBar is not a member of System.Windows.Forms.Form.

What am I doing wrong ?

The type of Me.MdiParent is System.Windows.Forms.Form. stBar is not a member
of the Form class. If the type of the child will always be the same, you
can use type casting:

Directcast(Me.MdiParent, <type name of the container>).stBar.Text = "some
text"


IMHO, the better approach is to raise an event in the child and catch it in
the MdiParent. In the event handler the parent sets the text of the statusbar.
 
F

Fergus Cooney

Hi Alex,

Ah yes, thanks for reminding me.

Me.MdiParent <will> give you correct parent Form but the Type of MdiParent
is plain old Form, whereas the actual Form is a derivation -
AlexesWonderfulAppForm, so the Controls are 'hidden'.

You'll need to cast the MdiParent to get at the additional properties.
Dim MyMainMan As AlexesWonderfulAppForm
MyMainMan = DirectCast (Me.MdiParent, AlexesWonderfulAppForm)
MyMainMan.stBar.Text = "Yo, Dude!!"

You can, of course, do all that in one go:
DirectCast (Me.MdiParent, AlexesWonderfulAppForm).stBar.Text _
= "Yo, Dude!!"

Regards,
Fergus
 
C

Chris Dunaway

alex said:
Fergus,
hmm... I have a status bar in the MdiParent and from the child calling
Me.MdiParent.stBar.Text = "some text" gives me the error

stBar is not a member of System.Windows.Forms.Form.

Make sure that stBar is either a property in your form class, or make the
variable public.

Chris
 
C

Cor

Hi Chris,
I have seen a lot of Fergus code, but making a variable just public,
somewhere alone on the top of his program.
I don't believe he likes that.
;-)
Cor
 
F

Fergus Cooney

Hi Cor, Chris

'Tis true, although I'll do it for expediency in some cases. ;-) I'll even
suggest it in an answer, if I think that the OP is inexperienced and would
reject the 'better' approach as being too complicated, or if I think that
expediency is their priority.

However, in this case the StatusBar is Friend which is visibility enough.
The problem is that MdiParent Property is of Type Form and doesn't have the
StatusBar.

Regards,
Fergus
 
A

alex

Fergus,
as you seem to be the expert here :) , how can I access the status bar
from a class that is called by a mdi child. i have implemented a class
that reads a file and i want to update the status bar from within that
class.

the direc cast me.mdiparent does not work ...

Thanks
Sorry , question might obvious... just started in vb.

Alex
 
C

Chris Dunaway

Hi Chris,
I have seen a lot of Fergus code, but making a variable just public,
somewhere alone on the top of his program.
I don't believe he likes that.

I agree with him but it was a quick, off the top of my head, answer.

Chris
 
F

Fergus Cooney

Hi Alex,

:) Flattery will get you everywhere, lol - except when I'm out for the
day!

|| the direc cast me.mdiparent does not work ...

Is that the one where the child is accessing the parent's StatusBar, ('cos
that works for me) or do you mean that using the same method with a different
class doesn't work?

If it's the first one, I'll need some code to see what's different between
what I've got here and what you've got.

Moving on...

If you have a separate class (I'll call it AlexFileReader) that needs to
use the StatusBar to display progress, the class needs a reference to it, or
to it's owner.

The simplest way, given that you're new to VB would be to tell the class
which StatusBar to play with. It depends where your AlexFileReader is created.
If the parent Form does this then it is very simple

In Main Form
Dim oAlexFileReader As AlexFileReader
oAlexFileReader = New AlexFileReader (Me.stBar)

In AlexFileReader
Private oStatusBar As StatusBar
Sub New (oThisStatusBar As StatusBar)
oStatusBar = oThisStatusBar
End Sub
Sub ShowProgress
oStatusBar.Text = "It's going great!!"
End Sub

If you are creating the AlexFileReader from an child Form, then you need
the full-typed reference to the main Form that we talked about earlier.

In Child Form
Dim MyMainMan As AlexesWonderfulAppForm
MyMainMan = DirectCast (Me.MdiParent, AlexesWonderfulAppForm)

Dim oAlexFileReader As AlexFileReader
oAlexFileReader = New AlexFileReader (MyMainMan.stBar)

If you are creating the AlexFileReader from a different class altogether,
then a bit more is needed. But I'm assuming that you aren't. ;-)

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top