wpfvb.netform-load

Have Sub run upon WPF Start


I am looking for how to create a private sub to load data from a txt file into some textboxes and the run a message box asking the user if the loaded data is correct. I haven't figured out to access the form_load or start_load of a WPF yet after much googling. Thanks to everyone in advance.

EDIT: Here is the code for the sub I want to add to the Form_Load of the WPF when it starts. I just have no clue how to add it.

    Private Sub Form_Load()
    'Load User data from CVTData.txt
    Dim DataPath As String = "C:\Temp\HBS-CVTv2.1\CVTData.txt"
    If System.IO.File.Exists(DataPath) Then
        Dim values() As String = File.ReadAllText("C:\Temp\HBS-CVTv2.1\CVTData.txt").Split("|"c)
        textbox1.Text = values(0)
        Textbox2.Text = values(1)
        TextBox3.Text = values(2)
        Textbox4.Text = values(3)

         Select MsgBox("Welcome to the CVT Utility.  This will take about 10 minutes to complete" & vbNewLine & "Please confirm that the data entered into the boxes is correct" & vbNewLine & "If so Press Yes" & vbNewLine & "If not Press No to enter data" & vbNewLine & "Press Cancel to close CVT Utility", MsgBoxStyle.YesNoCancel, "Application Start")
            'If Yes then Start Short Automated Run
            Case MsgBoxResult.Yes

                'Create c:\temp\logs folder
                Dim LogPath As String = "c:\temp\logs"
                If (Not System.IO.Directory.Exists(LogPath)) Then
                    System.IO.Directory.CreateDirectory(LogPath)
                End If

                'Run IPConfig on network
                Process.Start("cmd", "/c ipconfig > c:\temp\logs\pcinfo.txt")

                'Pause thread to let above process complete
                Threading.Thread.Sleep(2000)

                'Insert Date into TextBlockDate
                TextBlockDate.Text = Date.Today

                'Insert Time into TextBlockTime
                TextBlockTime.Text = Now.ToLongTimeString

                'Call Private Sub IPConfig_Write to update textblock1
                Call NetworkIP_Write()

                Call Auto_Wrk()

                'Toggle Buttons and Check Mark Image to advance to the next step
                Button6.IsEnabled = True
                Check1.Visibility = Windows.Visibility.Visible
                Check2.Visibility = Windows.Visibility.Visible
                Check3.Visibility = Windows.Visibility.Visible
                Check4.Visibility = Windows.Visibility.Visible
                Check5.Visibility = Windows.Visibility.Visible
                Button6.Background = System.Windows.Media.Brushes.Gold
                button1.IsEnabled = False

            Case MsgBoxResult.No

                'Create c:\temp\logs folder
                Dim LogPath As String = "c:\temp\logs"
                If (Not System.IO.Directory.Exists(LogPath)) Then
                    System.IO.Directory.CreateDirectory(LogPath)
                End If

                'Run IPConfig on network
                Process.Start("cmd", "/c ipconfig > c:\temp\logs\pcinfo.txt")

                'Pause thread to let above process complete
                Threading.Thread.Sleep(2000)

                'Insert Date into TextBlockDate
                TextBlockDate.Text = Date.Today

                'Insert Time into TextBlockTime
                TextBlockTime.Text = Now.ToLongTimeString

                'Call Private Sub IPConfig_Write to update textblock1
                Call NetworkIP_Write()

                'Toggle Buttons and Check Mark Image to advance to the next step
                button1.IsEnabled = True
                button1.Background = System.Windows.Media.Brushes.Gold

                'If Cancel Close Appliation
            Case MsgBoxResult.Cancel
                'Close Application
                Application.Current.Shutdown()
        End Select

    Else
        button1.IsEnabled = True
        button1.Background = System.Windows.Media.Brushes.Gold
        MsgBox("Welcome to the CVT Utility. Let's Begin")
    End If

End Sub

Solution

  • OK, I just tried it in an empty VB WPF project.

    If you want stuff to run when your WPF window is loaded, handle the Loaded event of your window:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
        <Grid>
           <!-- stuff -->
        </Grid>
    </Window>
    

    Then, in the code behind file, you can write your event handler:

    Class MainWindow
        Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
            'code here
        End Sub
    End Class