2015-02-17 73 views
1

我正在尋找如何創建一個私人子從txt文件加載數據到一些文本框,並運行一個消息框詢問用戶加載的數據是否正確。在Google進行了大量搜索之後,我還沒有想到要訪問WPF的form_load或start_load。感謝大家提前。有子控件運行WPF開始

編輯: 這是我想要添加到WPF的Form_Load啓動時的子代碼。我只是不知道如何添加它。

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 
+0

到目前爲止,你做了什麼,請添加一些代碼,告訴我們你已經投入了多少努力 - 其他問題可能會被關閉! – 2015-02-17 20:27:28

+0

@PilgerstorferFranz代碼已被添加到文章 – LordDaius 2015-02-17 21:10:37

+0

'Form_Load'看起來像Windows窗體給我。在WPF中,你有'Application.Startup'和'Window.Loaded'事件。對不起,我做C#不是VB,所以我不能更具體。 – vesan 2015-02-17 21:47:55

回答

2

好的,我只是在一個空的VB WPF項目中試過。

如果你想要的東西時,被載入您的WPF窗口中運行,處理你的窗口Loaded事件:

<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> 

然後,在代碼隱藏文件,你可以寫你的事件處理程序:

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

完美運作。謝謝! – LordDaius 2015-02-18 19:25:45