?
Solved

VB .NET Forms

Posted on 2004-10-10
3
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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