Pradip Dhakal
Pradip Dhakal

Reputation: 1962

How to find parent form name ? vb.net

I want the name of the parent form in vb.net . Where I came from.

Like

`if parent-form.name = 'something' then
    do something.
else
   do something else.`

It's not a MDI form.

Edit: I want exactly like that:

parentForm.vb:

chidform.showdialog()

In childform there is textbox

childForm.vb:

if parentForm is parentForm1 than fill textbox.text = 2 
else fill textbox.text = 3 

You will understand what I want.

Upvotes: 0

Views: 3922

Answers (1)

Fabulous
Fabulous

Reputation: 2423

Assumption: I worked under the assumption that this is an MDI application and you would want to read the name of the top parent and for reasons unknown you cannot refer to a singleton reference to the form (for instance current form runs from a library).

I wrote an extension mode to get the top parent since the immediate parent of an MDI child window is an MDIClient without a name.

<Extension>
Public Function GetTopParent(currentControl As Control) As Control
    Dim parent As Control = Nothing

    Do While currentControl IsNot Nothing
        parent = If(currentControl.Parent, parent)
        currentControl = currentControl.Parent
    Loop

    Return parent
End Function

Then when you need the name of the parent you can do the following.

MessageBox.Show(Me.GetTopParent().Name) ' This just shows the name but you can do your comparison here.

Upvotes: 2

Related Questions