Link to home
Start Free TrialLog in
Avatar of JeffN825
JeffN825

asked on

VB .NET Forms

My experience is with VB 6 and I have a question regarding VB .NET forms.  How do I refer to public variables and functions that exist within a form from another form, an instance of which has been instantiated by this form?  Simplified, I have a form frmMain which loads on application start.  I have another for frmHelp.  I have an event in frmMain which does:

dim frmHelp as New frmHelp
frmHelp.show

Now, how, from frmHelp, do I get the values of variables and call functions from the caller form, frmMain?  In VB6, I could just do:

frmMain.foo

from frmHelp.  Note that instantiating a new instance of frmMain within frmHelp is not an acceptable solution, given that I need access to variables whose default values have been changed.

Secondly, I need to make frmMain pause its activity until frmHelp has finished what it needs to do.  In other words, frmMain should pull up frmHelp, then wait until frmHelp has been closed to continue with the remaining instructions in the event function of frmMain which called frmHelp.

I hope this is clear.

Thanks.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

One way is to declare a public variable in frmHelp to reference frmMain.  After instantiating frmHelp, you set the variable to your frmMain.  To pause frmMain until frmHelp is closed, use ShowDialog() instead of Show():

Public Class frmHelp
    Inherits System.Windows.Forms.Form

    Public frmMainRef As frmMain

    Private Sub Foo()
        ' Do something with frmMainRef
        frmMainRef.Text = "Changed from frmHelp"
    End Sub

End Class

Public Class frmMain
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hlp As New frmHelp
        hlp.frmMainRef = Me
        hlp.ShowDialog()
    End Sub

End Class
Avatar of Hans Langer
Hans Langer

Hi
Other way is create the frmHelp contructor with the parameter frmParent, that with the finally of conserve the encapsuling concept.

Public Class frmHelp
    Inherits System.Windows.Forms.Form

    Private _frmParent As frmMain

    Public Sub New(byval frmParent  as frmMain)
        MyBase.New()
        InitializeComponent()
        _frmParent = frmParent  
    End Sub

    Public Sub Hello()
        Me._Parent.TextBox1.Text = "Hello"
    End Sub


End Class


Public Class frmMain
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frmHelp As New frmHelp(me)      
        frmHelp.show
        Me.hide
End Sub

End Class


GL
ASKER CERTIFIED SOLUTION
Avatar of J_Mak
J_Mak

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial