Solved

VB .NET Forms

Posted on 2004-10-10
3
156 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 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

17 Experts available now in Live!

Get 1:1 Help Now