user1760869
user1760869

Reputation:

How do I make a Button in VB.NET Allow a User to Select a File?

I am making a Crypter in Visual Basic Express Edition 2010, and I am experiencing some problems. I am trying to make it so the user clicks on a button in the GUI, and it allows them to pick a file to Crypt. Does anyone have any ideas on how to do that? Here is the code I originally used to make it, but it is not working :( All help is appreciated!

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

Thanks guys.

Upvotes: 2

Views: 617

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

The OpenFileDialog is a WPF wrapper of the Win32 Control this works for me.

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

Upvotes: 1

SLaks
SLaks

Reputation: 887453

OpenFileDialog1 is null.
You need to initialize it to a New OpenFileDialog().

Upvotes: 0

Related Questions