Rob Sherratt
Well-known member
Here is another tip which I hope will be helpful to others. If so, please let me know?
I just finished development of a large Windows Forms app using Visual Studio Express 2012 and VB.NET 4.5 under Windows 8.
As everyone knows, when you are developing a Windows Form application, the Visual Studio development environment creates a single default Form1 class which runs no code you write at all unless it is "event driven". You have to use some buttons or other click-event handlers to make your application do anything useful in response to user input. Running code when the Form1 class is instantiated is not documented by Microsoft. It is problematic to try to change the default behaviour from instantiating the Form1 class automatically, because otherwise you have to write extra Forms handling code yourself in a way that is also not well documented by Microsoft.
Because I'm too lazy to research more complex alternatives, the way I initialize my Windows Form app and run startup code without requiring user intervention is the following (simple template shown below):
The event "Me.Activated" is raised by VB.NET 4.5 automatically whenever the user first runs the forms application. Unfortunately it is also generated whenever a FileDialog box or MessageBox is closed by the user. This can be a real pain since you don't want your "Me.Activated" code to run under those circumstances. Therefore I use the boolean flag "FormStarted" which is Globally declared within the Form1 class, and this ensures the Do_Initialize() code runs once only. You must not use any dialog box that involves user intervention within the Do_initialize "Me.Activated" procedure. You would get input from users following another user event such as a button click.
If you find this useful, please give me a good rating via "rate this thread" ?. I am anxious to get beyond the "Newbie" status on this forum :very_drunk:
Best regards to all,
Rob
I just finished development of a large Windows Forms app using Visual Studio Express 2012 and VB.NET 4.5 under Windows 8.
As everyone knows, when you are developing a Windows Form application, the Visual Studio development environment creates a single default Form1 class which runs no code you write at all unless it is "event driven". You have to use some buttons or other click-event handlers to make your application do anything useful in response to user input. Running code when the Form1 class is instantiated is not documented by Microsoft. It is problematic to try to change the default behaviour from instantiating the Form1 class automatically, because otherwise you have to write extra Forms handling code yourself in a way that is also not well documented by Microsoft.
Because I'm too lazy to research more complex alternatives, the way I initialize my Windows Form app and run startup code without requiring user intervention is the following (simple template shown below):
VB.NET:
Imports System.IO
Imports System.Net
Imports System.Data
Imports System.Threading
Imports System.Globalization
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Public Class Form1
' Global declarations go here, e.g.
Dim MyDataSet As New DataSet("All Data Tables")
Dim MyUpperTable As New DataTable("Input Worksheet.csv")
Dim MyLowerTable As New DataTable("Output Worksheet.csv")
Dim FormStarted As Boolean = False
Dim MyApplicationFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Const MyAppExceptionText As String = "Something really amazing is about to happen, ..."
Private Sub Do_Initialize(sender As Object, e As EventArgs) Handles Me.Activated
Try
If Not FormStarted Then
FormStarted = True
' Your initialization code goes here, an example shown of making a DataGridView display something useful before
' the Windows Form is used by the user. It will run once only when the Windows Form app starts up
' Make sure any DataTables are declared globally as in this example, so you can use them elsewhere.
MyInputFile = MyApplicationFolder & "\Input Worksheet Default.csv"
If Not CSV_To_Table(MyInputFile, MyUpperTable, True) Then
Console.WriteLine("Warning: The .csv file ""Input Worksheet Default.csv"" is missing and has not been displayed.")
MyDataGridView.DataSource = MyUpperTable
MyDataGridView.Show()
Thread.Yield()
End If
End If ' Not FormStarted
Exit Sub
Catch ex As Exception
Console.Writeline(MyAppExceptionText)
Console.Writeline(ex.ToString())
Exit Sub
End Try
End Sub 'Do_initialize
' Button click event handlers etc. go here
End Class 'Form1
The event "Me.Activated" is raised by VB.NET 4.5 automatically whenever the user first runs the forms application. Unfortunately it is also generated whenever a FileDialog box or MessageBox is closed by the user. This can be a real pain since you don't want your "Me.Activated" code to run under those circumstances. Therefore I use the boolean flag "FormStarted" which is Globally declared within the Form1 class, and this ensures the Do_Initialize() code runs once only. You must not use any dialog box that involves user intervention within the Do_initialize "Me.Activated" procedure. You would get input from users following another user event such as a button click.
If you find this useful, please give me a good rating via "rate this thread" ?. I am anxious to get beyond the "Newbie" status on this forum :very_drunk:
Best regards to all,
Rob
Last edited: