Solved

Starting Application from Module - how to show main form

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

20 Experts available now in Live!

Get 1:1 Help Now