Solved

Starting Application from Module - how to show main form

Posted on 2004-09-27
5
236 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

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

696 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