Hi @Mansour_Dalir ,
You can recursively traverse the parent hierarchy until you find a form.
Public Module UserControlExtensions
<Runtime.CompilerServices.Extension()>
Public Function GetTopLevelForm(control As Control) As Form
Dim parentForm As Form = TryCast(control, Form)
While parentForm Is Nothing AndAlso control IsNot Nothing
control = control.Parent
parentForm = TryCast(control, Form)
End While
Return parentForm
End Function
End Module
Then use this extension method on your UserControl instances like this:
Dim topLevelForm As Form = UserControlExtensions.GetTopLevelForm(Me)
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.