Welcome to weblogs.com.pk Sign in | Join | Help

Adnan Siddiqi

Lets get technical


  • View Adnan Siddiqi's profile on LinkedIn
.NET ::Accessing MDI form controls from child forms or other methods

I remember in VB6 days, it was damn easy to access other form controls by writing formName.ControlName.Property.This is something which is not possible in VB.NET. I tried traditional methods to access MDI form controls but got Object Reference error. Then I found solution by using DirectCast() method to assign values to MDI's controls at run time. So If you want to set statusbar on MDI from Child form, just write the following line:


DirectCast(Me.MdiParent, MainForm).StatusBar1.Text = "My Statusbar Value" 

 Here MainForm is the name of your MDI Form

As it's quite clear, here ME means the form where this code exist so if you are calling on Click Event of any form, use Me.MdiParent as first parameter and MainForm(MDI Form's)'s name as 2nd parameter. That's it!


If you want to access MDI controls from some other method in any other class then you can simply use any variable of type Form to perform the task since it just needs ANY child's form MDI property to perform the task. For example:

 DirectCast(myChildForm.MdiParent, MainForm).StatusBar1.Text = "My Statusbar Value"



Posted: Thursday, December 13, 2007 1:45 AM by kadnan
Filed under: , , ,


khurram said:

Your "StatusBar1" has to be public for this, which is not a good OOP practice...

Consider using "Events", as described at http://weblogs.com.pk/khurram/archive/2006/06/29/4321.aspx, same applies to Winforms too. With .NET 2's Anonymous Methods; things are even more easier, read http://msdn.microsoft.com/msdnmag/issues/06/00/c20/default.aspx

# December 13, 2007 10:16 AM
Anonymous comments are disabled