【问题标题】:Have Sub run upon WPF Start在 WPF 启动时运行 Sub
【发布时间】:2015-04-18 16:17:51
【问题描述】:

我正在寻找如何创建一个私有子以将 txt 文件中的数据加载到某些文本框中,并运行一个消息框,询问用户加载的数据是否正确。经过多次谷歌搜索后,我还没有想出访问 WPF 的 form_load 或 start_load 。提前感谢大家。

编辑: 这是我想在 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

【问题讨论】:

  • 到目前为止你做了什么,请添加一些代码,告诉我们你付出了哪些努力 - 否则这个问题可能会被关闭!
  • @PilgerstorferFranz 代码已添加到帖子中
  • Form_Load 在我看来就像 Windows 窗体。在 WPF 中,您有 Application.StartupWindow.Loaded 事件。抱歉,我做的是 C# 而不是 VB,所以我不能说得更具体。
  • @vesan 好的。代码在 C# 中的外观如何?也许我可以从你的代码中获取我需要的东西并获得一个工作功能。

标签: wpf vb.net form-load


【解决方案1】:

好的,我只是在一个空的 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

【讨论】:

  • 完美运行。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多