Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

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…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

580 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