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):

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:






Comments (2) -

Deanna Shearsmith
Deanna Shearsmith United States
6/9/2014 10:17:39 PM #

Have a excellent day!  Your WordPress is verty useful and nice Game information .  What? And We Make HomePage And WebSite AT <A href="">;ホ�Eムペ�Eジ作�E</A>  . If you want to get more pageview , you can contact us .

Sarah Jacqueline Marion Renshaw
Sarah Jacqueline Marion Renshaw United States
8/3/2014 10:34:14 AM #

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X,  And We Make HomePage And WebSite AT�@<A href=""></A>;  but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

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. Then SharePoint was a daily occupation for me. I implemented a large quality document management system at PMRL being the only developer/technical analyst. Working close together 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. One of my last projects with the consultancy was creating a custom LIMS system in c#.Net. This was a success 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 developing 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 ( 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.

I'm working for 2 years with objective C now. Creating apps for factories across Europe. People in the factory are working with IPad to take pictures and upload them to orders and more. 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. Next generation apps are build with a .Net Web API background combined with EF 6.0.

Soon I will start a new challenge at BDO. In preparation, I started to look into AngularJS. And I'm liking it. It's very fast to use, lots of documentation and works like a charm. Let's see if I'll still be as enthusiastic in a few months. I almost finished my first AngularJS web site. 

My linkedIn!

Month List

Tag cloud

Locations of visitors to this page