Solved

Starting Application from Module - how to show main form

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now