Getting Parent Form's Property Values in a Child Form
I have a custom form that I use instead of msgbox for a number of reasons. The initial reason was to keep the message centered on it's parent rather than the screen. There are many articles about this but I still have not found an elegant way to get access to the Parent's properties from inside the Child without passing them in (by one way or another).
In the child form the me.parent, me.parentform are not instantiated and apparently can not be used.
The point is to have this form be totally encapsulated with the logic being self contained. This is not an MDI child, and I do not want it to be due to the limitations on MDI children.
My current workaround looks like this I use a ShowMsg Function in my MyMsgBox form which looks like this:
VB.NET Code:
Friend Function ShowMsg(ByVal MYParent As Form, _
ByVal msg As String, _
Optional ByVal s As MsgBoxStyle = MsgBoxStyle.OkOnly, _
Optional ByVal Alignment As System.Windows.Forms.HorizontalAlignment = _
HorizontalAlignment.Center, _
Optional ByVal ItIsDialog As Boolean = True, _
Optional ByVal enCut As Boolean = False, _
Optional ByVal eMBoxBRColor As Object = Nothing, _
Optional ByVal eMBtxtBRColor As Object = Nothing) As MsgBoxResult
m_Style = s
m_Msg = msg
m_Alignment = Alignment
m_EnableCut = enCut
If eMBoxBRColor <> Nothing Then m_meBackroundColor = eMBoxBRColor
If eMBtxtBRColor <> Nothing Then m_txtBackroundColor = eMBtxtBRColor
m_ParentWidth = MYParent.Width
m_ParentHeight = MYParent.Height
m_ParentTop = MYParent.Top
m_ParentLeft = MYParent.Left
If ItIsDialog Then
ShowMsg = Me.ShowDialog
Else
Me.Show()
End If
End Function
The Call from the Parent looks like this
VB.NET Code:
MyMsgBox.ShowMsg( Me, "This is my Message")
My question, Is there a more elegant way given the me.parent.properties seem to not be available in the child?
Re: Getting Parent Form's Property Values in a Child Form
Moved From The Codebank (which is for sharing code rather than posting questions :) )
Re: Getting Parent Form's Property Values in a Child Form
You can access base properties of the parent form as follows
Code:
Dim f As New frmChild
f.Owner = Me
Try
f.ShowDialog()
Finally
f.Dispose()
End Try
In child form, get and set properties
Code:
Me.Owner.Top = 0
MessageBox.Show(Me.Owner.Text)
If you want to access controls you will need to be more specific especially if many forms may be calling this custom form.
Re: Getting Parent Form's Property Values in a Child Form
Very nice, owner property will accept the assignment of a value in the load event of the parent form, and everything seems to work... getting stuck on parentform and parent were my mistake...
Great Site, some super nice advice so far. Hope I can help someone at sometime...
Re: Getting Parent Form's Property Values in a Child Form
I seem to have found a problem with Kevin's solution as applied to my situation which might be worth Describing.
I instantiate the form in the declarations at the top of the Program and load the (me) as first step in the parent form. All is well and good, However,
It appears as though (and this now seems obvious) that the child form does not automatically reset to default starting parameters because you don't close it (in which case it's context is lost and it needs to be re-instantiated).
So be careful in your child to reinitialize the start up conditions every time you enter it.
In my case, text-box and form background colors and the 3 buttons ('yes/ok', 'no', or 'cancel') positions and visibility had to have more attention paid to them.
Re: Getting Parent Form's Property Values in a Child Form
Quote:
Originally Posted by
RayLNelson
I seem to have found a problem with Kevin's solution as applied to my situation which might be worth Describing.
I instantiate the form in the declarations at the top of the Program and load the (me) as first step in the parent form. All is well and good, However,
It appears as though (and this now seems obvious) that the child form does not automatically reset to default starting parameters because you don't close it (in which case it's context is lost and it needs to be re-instantiated).
So be careful in your child to reinitialize the start up conditions every time you enter it.
In my case, text-box and form background colors and the 3 buttons ('yes/ok', 'no', or 'cancel') positions and visibility had to have more attention paid to them.
Yes that is how I would expect it to be used which means there is no problem, generally speaking when one displays a child form it is modal and disposed of after ShowDialog.
Re: Getting Parent Form's Property Values in a Child Form
I am using it as a replacement for MsgBox. I did not want to have to constantly reinstantiate it...
Internal to the module, I save the initial conditions that change, like background for form and text and restore them as we exit the form and I don't close it.
That way I don't have to do anything special to call it again, just execute the MyMsgBox.ShowMsg which is in the child (eMsgBox).
So I did find a solution that so far maintains the goal of having it run almost as a replacement for the MsgBox VB call. (I don't support all the MsgBox styles, but I do allow things like cutting from the textbox.)
Seems pretty good now...