如何在VB.NET中创建一个按钮允许用户选择文件?

时间:2012-10-21 01:08:11

标签: .net wpf vb.net

我在Visual Basic Express Edition 2010中制作了一个Crypter,我遇到了一些问题。我试图让它用户点击GUI中的按钮,它允许他们选择一个文件到Crypt。有没有人对如何做到这一点有任何想法?这是我最初用来制作它的代码,但它不起作用:(感谢所有帮助!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    With OpenFileDialog1
        .FileName = ""
        .Filter = "Executables (*.exe)|*.exe|All files (*.*)|*.*"
        .Title = "The Justice Crypter"
        .ShowDialog()
        TextBox1.Text = .FileName
        infectedfile = TextBox1.Text
    End With
End Sub

谢谢你们。

2 个答案:

答案 0 :(得分:1)

OpenFileDialog是Win32控件的WPF包装,这对我有用。

Class MainWindow 
    Dim WithEvents openFileDialog1 As New Microsoft.Win32.OpenFileDialog
    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        With OpenFileDialog1
            .FileName = ""
            .Filter = "Executables (*.exe)|*.exe|All files (*.*)|*.*"
            .Title = "The Justice Crypter"
            .ShowDialog()
            TextBox1.Text = .FileName
            'infectedfile = TextBox1.Text
        End With
    End Sub
End Class

答案 1 :(得分:0)

OpenFileDialog1为空。
您需要将其初始化为New OpenFileDialog()