Getting Parent Form's Property Values in a Child Form-VBForums
Results 1 to 7 of 7

Thread: Getting Parent Form's Property Values in a Child Form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Location
    Las Vegas, NV
    Posts
    18

    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:
    1. Friend Function ShowMsg(ByVal MYParent As Form, _
    2.                             ByVal msg As String, _
    3.                    Optional ByVal s As MsgBoxStyle = MsgBoxStyle.OkOnly, _
    4.                    Optional ByVal Alignment As System.Windows.Forms.HorizontalAlignment = _
    5.                                           HorizontalAlignment.Center, _
    6.                    Optional ByVal ItIsDialog As Boolean = True, _
    7.                    Optional ByVal enCut As Boolean = False, _
    8.                    Optional ByVal eMBoxBRColor As Object = Nothing, _
    9.                    Optional ByVal eMBtxtBRColor As Object = Nothing) As MsgBoxResult
    10.  
    11.         m_Style = s
    12.         m_Msg = msg
    13.         m_Alignment = Alignment
    14.         m_EnableCut = enCut
    15.  
    16.         If eMBoxBRColor <> Nothing Then m_meBackroundColor = eMBoxBRColor
    17.         If eMBtxtBRColor <> Nothing Then m_txtBackroundColor = eMBtxtBRColor
    18.  
    19.         m_ParentWidth = MYParent.Width
    20.         m_ParentHeight = MYParent.Height
    21.         m_ParentTop = MYParent.Top
    22.         m_ParentLeft = MYParent.Left
    23.  
    24.         If ItIsDialog Then
    25.             ShowMsg = Me.ShowDialog
    26.         Else
    27.             Me.Show()
    28.         End If
    29.  
    30.     End Function

    The Call from the Parent looks like this

    VB.NET Code:
    1. 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?
    Last edited by RayLNelson; Feb 20th, 2012 at 11:42 AM.

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Getting Parent Form's Property Values in a Child Form

    Moved From The Codebank (which is for sharing code rather than posting questions )
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,933

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Location
    Las Vegas, NV
    Posts
    18

    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...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Location
    Las Vegas, NV
    Posts
    18

    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.

  6. #6
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,933

    Re: Getting Parent Form's Property Values in a Child Form

    Quote Originally Posted by RayLNelson View Post
    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Location
    Las Vegas, NV
    Posts
    18

    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...

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.