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

Posted on 2005-03-12
Medium Priority
Last Modified: 2011-09-20
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????

Question by:omarmallat
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
1 Comment
LVL 86

Accepted Solution

Mike Tomlinson earned 200 total points
ID: 13524455
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

    ' 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

    ' 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
            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
        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)
        End Sub
    End Class

(5) Build your app using the ApplicationContext class and a similar technique to that presented in Method (4):


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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