what is the perfect way to show the main form from a module sub main

my startup project is a sub main in a module. after my personel security test I will load the main form. the main form contains a status bar with a progress bass and a cancel button.
what I need is to affect the value of the progress bar from each form that need that, and if the cancel button was clicked, i'll stop the process.
the problem is:
in the sub main I couldn't write FrmMain.show or application.Run(FrmMain).
I wrote:
dim f as frmMain.
frmMain.show' or ShowDialog

but in this case, I can know the form f from the other form.
I can declare f in the module as public.
but is there any other way to do that. or this is the perfect way????

thank
OmarMallat
omarmallatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
There are several ways you can go about this.

(1) Declare f as public in a module so it's accessible from anywhere.

(2) Use the .Owner property when you create your secondary forms and cast it FrmMain from within your secondaries:

    ' From within FrmMain:
    Dim f2 As New Form2
    f2.Owner = Me
    f2.Show()

    ' From within Form2:
    Dim f As FrmMain = CType(Me.Owner, FrmMain)
    f.ProgressBar1.Value = f.ProgressBar1.Value + 1

(3) Declare a public variable in your secondary forms and set it when you create them:

    ' From within FrmMain:
    Dim f2 As New Form2
    f2.f = Me
    f2.Show()

    ' In Form2:
    Public Class Form2
        Public f As FrmMain

        Private Sub foo()
            If Not (f Is Nothing) Then
                f.ProgressBar1.Value = f.ProgressBar1.Value + 1
            End If
        End Sub
    End Class

(4) Declare an Interface that all of your secondary forms must Implement.  Then FrmMain can subscribe to these events using AddHandler() and get messages from them:

    ' All of your secondary forms must implement this Interface:
    Public Interface SecondaryForm
        Event ProgressChanged(ByVal sender As Form, ByVal value As Integer)
        Event Cancelled(ByVal sender As Form)
    End Interface

    ' example of FrmChild implement the interface and raising the events:
    Public Class FrmChild
        Inherits System.Windows.Forms.Form
        Implements SecondaryForm

        Public Event Cancelled(ByVal sender As System.Windows.Forms.Form) Implements SecondaryForm.Cancelled

        Public Event ProgressChanged(ByVal sender As System.Windows.Forms.Form, ByVal value As Integer) Implements SecondaryForm.ProgressChanged

        Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
            RaiseEvent ProgressChanged(Me, TrackBar1.Value)
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Close()
            RaiseEvent Cancelled(Me)
        End Sub
    End Class

    ' example of FrmMain creating and subscribing to the secondary form events:
    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 fc As New FrmChild
            AddHandler fc.ProgressChanged, AddressOf Me.sf_ProgressChanged
            AddHandler fc.Cancelled, AddressOf Me.sf_Cancelled
            fc.ShowDialog()
        End Sub

        Private Sub sf_ProgressChanged(ByVal sender As System.Windows.Forms.Form, ByVal value As Integer)
            ProgressBar1.Value = value
        End Sub

        Private Sub sf_Cancelled(ByVal sender As System.Windows.Forms.Form)
            MsgBox("Cancelled")
        End Sub
    End Class

(5) Build your app using the ApplicationContext class and a similar technique to that presented in Method (4):
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21254498.html
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21274481.html

~IM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.