Solved

Starting Application from Module - how to show main form

Posted on 2004-09-27
5
238 Views
Last Modified: 2010-04-23
I have researched and tried the methods I found regarding using a module
as the startup object.  I am apparently missing something.

ModuleBV is my startup object; Form1 is the main form I want to show.  No matter what I try, I
get err 91 - object reference not set to an instance of an object when I get to line frmMain.Show()

Thank you,

Ruth


Here is my code:

Option Explicit On
Option Strict On
Imports System.windows.forms

Module ModuleBV

    ' make all forms public can then use show and hide from whereever
    Public frmMain As New Form1
    Public Review As New frmReview

    ' make users dataset public
    Public dsUsers As DataSet
    Public Sub Main()

       Try
           ' build connection
            'populate dataset
       catch
             messagebox.show(err.number)
             messagebox.show(err.description)    
       Finally
             'close connection
        Dim frmMain As New Form1
        frmMain.Show()
    End Sub
End Module

0
Comment
Question by:RuthD2004
[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
  • 2
5 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12160280
you define frmmain twice

Module ModuleBV

    ' make all forms public can then use show and hide from whereever
    Public frmMain As New Form1  '  **** <-- HERE
    Public Review As New frmReview

    ' make users dataset public
    Public dsUsers As DataSet
    Public Sub Main()

       Try
           ' build connection
            'populate dataset
       catch
             messagebox.show(err.number)
             messagebox.show(err.description)    
       Finally
             'close connection
        Dim frmMain As New Form1 '****** AND HERE
        frmMain.Show()
    End Sub
End Module
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12160281
you realize you initialize frmMain twice?

once before the call, and the other under your public declarations...
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12160329
Sorry, that shouldn't matter (although it is wrong).

Are you sure this is the exact module you are using, because it is full of mistakes
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12160645
RonaldBiemans - your absolutely right it shouldnt matter.  

your also right this code has to be incomplete....becuase there is no 'End Try' clause

in the following code i added that, and changed Show() to ShowDialog, and it works for me...

Module ModuleBV

    ' make all forms public can then use show and hide from whereever
    Public frmMain As New Form1
    Public Review As New frmReview

    ' make users dataset public
    Public dsUsers As DataSet
    Public Sub Main()
        Try
            ' build connection
            'populate dataset
        Catch
            messagebox.show(err.number)
            messagebox.show(err.description)
        Finally
            'close connection
            frmMain.ShowDialog()
        End Try
    End Sub

End Module

~b
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12160733
Hi Bramsquad,

The end try isn't the only mistake, it look like this code was copied from a vb6 application.

RuthD2004,

This is the proper way to start from a module

Imports System.Windows.Forms

Module MainModule
    Public mainForm As mainForm
    Private appContext As ApplicationContext

    Sub Main()
        StartApplication()
        ShowMainForm()
        ReadEvents()
        Cleanup()
    End Sub

    Sub StartApplication()
        appContext = New ApplicationContext()
    End Sub


    Sub ShowMainForm()
        mainForm = New MainForm()
        mainForm.Show()
    End Sub

    Sub ReadEvents()
        appContext.MainForm = mainForm
        Application.Run(appContext)
        ' Application will exit when closing mainForm
    End Sub

    Sub Cleanup()
        ' Garbage collection here
    End Sub
End Module
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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