Dan Tao
Dan Tao

Reputation: 128317

Is there any way to display a Windows form BEFORE the "Startup" form is loaded in VB.NET?

My company's main software package includes a hefty configuration library which loads on startup. This config library includes some mandatory settings which, if not supplied (via command line arguments), cause the entire application to exit.

This has never been an issue for our users, who launch the software via scripts which have the needed command line arguments supplied automatically. But sometimes when debugging our software we developers forget to specify the necessary arguments in Visual Studio's debug options; it's then very annoying to be greeted with the message Config specification invalid -- missing required parameters X, Y, and Z -- shutting down (I'm paraphrasing, of course).

It's not really a big deal, just an annoyance. Still, I felt it worthwhile to throw together a little form to make this process a little less painful; it notifies the user which parameters are missing and allows him/her to specify values for those parameters directly on the form, without having to restart the application.

My intentions were good (I think?), but it seems I can't get this solution to actually work. The problem is that after I've launched our software with missing settings, the form pops up and prompts me as expected; but after I've entered the required parameters and it's time for the application to "really" start, I get this InvalidOperationException:

SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.

I think I understand what's going on here: the VB.NET project I'm working on is doing something like this "behind the scenes"*:

Sub Main()
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New MainForm)
End Sub

That call to SetCompatibleTextRenderingDefault is, apparently, throwing an exception because a form was already created and displayed prior to its execution.

Is there any way around this? Is there perhaps a more "proper" solution to this problem that I'm not thinking of (i.e., should I not be trying to collect user input via a form at all)?

*This is a best guess based on what I've seen in C# WinForms projects. Strangely, unless I'm missing something, it seems that VB.NET WinForms projects completely hide this from the developer.

Upvotes: 0

Views: 1641

Answers (2)

Hans Passant
Hans Passant

Reputation: 941237

Do make sure that you have the application framework option turned off and Sub Main selected as the starting method. Make it look similar to this:

Sub Main(ByVal args() As String)
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    If args.Length = 0 Then
        Using dlg As New OptionsDialog
            If dlg.ShowDialog <> DialogResult.OK Then Return
            '' Use dlg result...
        End Using
    End If
    Application.Run(New MainForm)
End Sub

Upvotes: 3

Reddog
Reddog

Reputation: 15579

Perhaps you could use the static Debugger.IsAttached (or even a #DEBUG directive) in your program's "main" function that feeds in some input file (say an XML file) into your parsed args collection instead?

Upvotes: 0

Related Questions