Solved

VB .NET Forms

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

717 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