Solved

VB .NET Forms

Posted on 2004-10-10
3
163 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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