vb.netshowdialogowner

Get parent form name value in VB.NET


Hi I have a form which is called by several other forms via the 'showdialog()' command I'd like it to do different things depending on which form it's called from

so I need to get the name of the parent form. I try with Me.Parent.Name or Me.Owner.Name I always get nothing value.

how can I get the form father that called the showdialog command?


Solution

  • There is no parent. That is only relevant for child controls. If you add a Button to a form, the form is the parent of the Button. Your form, like most, is a top-level window, so it has no parent.

    There is no owner unless you specify one. The way to do that is to pass the owner when you call ShowDialog, i.e.

    Using dialogue As New DialogueForm
        dialogue.ShowDialog(Me)
    End Using
    

    The dialogue will then be able to access the calling form via its Owner property.

    Note that you shouldn't be interested in the Name of the owner but rather its type, e.g.

    Dim ownerType = Owner.GetType()
    
    If ownerType Is GetType(Form1) Then
        '...
    End If