How to get a user controls parent form. (Windows forms)

by Mark Deraeve 3. July 2012 05:12

I wanted to pass the owner form to all my ShowDialog calls. But lots of times I call this method from a user control. So I found a good solution on stackoverflow (it’s a recursive function that will loop over all parents and check if it’s a windows form): http://stackoverflow.com/questions/371464/get-components-parent-form

I changed it a little to fit my needs, also this project was VB.Net:

   

''' <summary>
    ''' Returns the parent System.Windows.form of the control
    ''' </summary>
    ''' <param name="parent"></param>
    ''' <returns>First parent form or null if no parent found</returns>
    ''' <remarks></remarks>
    Public Shared Function GetParentForm(ByVal parent As Control) As Form
        Dim form As Form = TryCast(parent, Form)
        If form IsNot Nothing Then
            Return form
        End If
        If parent IsNot Nothing Then
            ' Walk up the control hierarchy
            Return GetParentForm(parent.Parent)
        End If
        Return Nothing
        ' Control is not on a Form
    End Function

 

 

I just call this method new on all windows and user controls where I have a show dialog call:

 

frm.ShowDialog(GP_Global.GlobalFunctions.GetParentForm(Me))

 


Tags:

|

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

I started as a VB6 developer, programming all kinds of applications. I also got involved in the development of a e-market in ASP.

When .Net was introduced, I had the privilege to start in a team of professionals and develop a framework. I learned a lot and started to work as a c#.Net consultant from then on.

After 2 years I also got the chance to work for several months with Sharepoint. Sharepoint kept chasing me all the years after and when the product got more and more mature I started to love it. Now Sharepoint is a somewhat daily occupation for me. I implemented a large quality document management system at PMRL being the only developper/technical analyst. Working close togehter with the QA department we succeeded in making and improving a very good Quality document management system. Its used up until this day.

I worked for 6 years as a .Net consultant for 2 of the largest consultancy companies in Belgium. I mainly worked as a Sharepoint engineer, but also .Net expert and several ASP.Net and Windows applications. One of my last projects with the consultancy was creating a custom LIMS system. This was a succes and the customer asked me to stay and help to expand and maintain this LIMS. I had great years in this company. There was a focus on innovation and a lot of opportunities for improving and developping new IS systems. Apart from several small projects I further more improved the custom LIMS system, created a SOP library based on Sharepoint and a web based Skill matrix system to handle the skills of the employees in the company. 

Then I was involved in transferring the LIMS to a new lab in Singapore. This was done in a short time frame. The deployment was again a success and it passed the GLP inspection tests. I learned a lot from the Quality engineers about validation of computerized systems.

At my current job I have a wide range of challenges. Mainly I'm still working with .Net (VB.net and C#.Net). But outside of the Microsoft world I also do some ABAB programming in our SAP system. Like developing Adobe Forms and the program behind it. Or making small changes to customized code.

Latest I started to follow a course from Stanford Univercity on Objective C programming for IPAD and IPHONE.I just finished the first app for IPad and it appears to be a great succes. People in the factory are working with IPad to take pictures and upload them to orders. Already more and more projects are coming my way. The iOS apps are supported by a home made .Net architecture that exposes WCF services. Its working great. My second app is almost ready for deployment in factories. Now I started an App for the management team so they can manage projects on their IPAD and IPHONE.

My linkedIn!

Month List

Tag cloud

Locations of visitors to this page