WindowsForms QuickTip: Me.Owner
Want to access the parent form when using ShowDialog to show a form? Consider the following where Form1 is the parent and Form2 is the child to be shown modally.
Dim Form2 As New Form2Form2.ShowDialog()
Form2.Dispose()
If this is in a Button.Click in Form1, this will show Form2 modally from Form1. Now say you need to access Form1 from Form2 to possibly call a Public Method to set it's StatusBar.Text or whatever.
DirectCast(Me.Owner, Form1).SetStatusBarText("I own you!")Unfortunately, this doesn't "just work" like I'd expect (maybe you do too). So you can do one of two things to get Owner to return the Form that is showing it modally. Either manually set Form2.Owner = Me before calling ShowDialog or just pass Form1 into ShowDialog like so.
Dim Form2 As New Form2
Form2.ShowDialog(Me)
Form2.Dispose()
Doing it this way still makes it feels sort of "built-in" in my opinion and allows you to access the parent form without having to setup your own properties.
1 Comment
Hobb.. said
January 05, 2007
Thanks helped me with a problem i've been stewing over all week.