Solved

VB .NET Forms

Posted on 2004-10-10
3
157 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:JeffN825
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12273847
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
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12273940
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
0
 
LVL 7

Accepted Solution

by:
J_Mak earned 250 total points
ID: 12274066
For your first question:
You could always create a property named 'foo' in your frmHelp which accesses the appropriate frmMain form. For example:

Public Class frmHelp
    Inherits System.Windows.Forms.Form

    Private mainForm as frmMain 'this is kept private, but can be accessed using the below property

    Friend  Property MainFormProperty() As frmMain
        Get
            MainFormProperty = mainForm
        End Get
        Set(ByVal Value As frmMain)
            mainForm = Value
        End Set
    End Property

    ' frmHelp can then set frmMain's variable, 'someVariable'
    MainFormProperty.stringVariable = "whatever"

   ' or, frmHelp can get the value of 'someVariable'
   Dim strVar as String = MainFormProperty.stringVariable

End Class

And in your frmMain, do this:

Public Class frmMain
    Inherits System.Windows.Forms.Form
    ..... ' some other commands

        Private someVariable as String

        Friend  Property stringVariableProperty() As String
            Get
                stringVariableProperty = someVariable
            End Get
            Set(ByVal Value As String)
                mainForm = Value
            End Set
        End Property

        Dim frmHelp as New frmHelp
        frmHelp.MainFormProperty = Me
        frmHelp.ShowDialog()       

    ..... ' some other commands
End Class

Properties are a great way of accessing member variables of other forms. Try it out, it is good practice. I have tested the above solution, but I'm pretty sure it will work. I'll leave it up to you to fix it if required, but it should be accurate.

For you second question:
You need to open your frmHelp form in modal form. I've already done it for you, using ShowDialog() rather than just Show. I hope that helps!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short film showing how OnPage and Connectwise integration works.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now