Andrew
Andrew

Reputation: 477

Do not open form when passing arguments

I have a typical form with listboxes, textboxes, buttons and so on. This form essentially loads configuration files (.cfg) and populates the objects. The user can then 'generate' a report based upon the content (from the file) that is now in the objects.

However - I want the user to be able to use commandline parameters to load the .cfg file and generate the report. The caveat here is that it would be easier to still have the objects load on the screen (so that I don't have to create more code to generate the report.

To do this, so far, I created this code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
  If my.Application.CommandLineArgs.count > 0 then
    me.Hide()
    with my.Application
      'Load the specific file
      LoadCfgFile(.CommandLineArgs(0))

      'GenerateReport
      GenerateCSVReport()

      'Exit Application
      application.Exit()
    end with
 End If
End Sub

The problem is that the form does show up for a split second as the report is being generated and I would prefer it never show itself at all when running with parameters.

Upvotes: 0

Views: 76

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54447

The proper place to do this is in the Startup event handler of the application, not the startup form.

Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
    ' The following events are available for MyApplication:
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
            If e.CommandLine.Count > 0 Then
                'Do whatever here.
                '...

                'Exit without creating the startup form.
                e.Cancel = True
            End If
        End Sub

    End Class
End Namespace

You can access that code file from the Application page of the project properties.

Upvotes: 1

Related Questions