OpenFileDialog in WPF using VB.NET

This article shows openfiledialog in WPF using VB.NET.
  • 4038

This article shows OpenFileDialog in WPF using VB.NET.


The OpenFileDialog is used to select a file. These are the some common properties of OpenFileDialog class.

File name

The FileName Property is used to get or set file name selected in the file dialog box.


The Filter Property is used to get or set the current file name filter string which sets the choices that appear in 'Save as file type' or 'Files of type' box.

ShowDialog method

To display the dialog box to the user, call the ShowDialog() method.

For example:

Drag and drop one Button, one ListBox and one Image control on the form.


Figure 1.

XAML code

<Window x:Class="MainWindow"



    Title="MainWindow" Height="350" Width="525">


        <Image Height="150" HorizontalAlignment="Left" Margin="52,12,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />

        <Button Content="Openfile" Height="23" HorizontalAlignment="Left" Margin="52,198,0,0" Name="Button1" VerticalAlignment="Top" Width="167" />

        <ListBox Height="100" HorizontalAlignment="Left" Margin="292,100,0,0" Name="ListBox1" VerticalAlignment="Top" Width="120" />



Now, double click on the button named openfile in the form and add the following code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Dim myDialog As New OpenFileDialog()

        myDialog.Filter = "All Files|*.*|Image Files|*.jpg;*.gif;*.png;*.bmp"


        Image1.Source = New BitmapImage(New Uri(myDialog.FileName, UriKind.Absolute))


    End Sub

Source class is used to load the image dynamically.

Now, run the application and test it.

Now, click on the Openfile Button and select a image.


Figure 2.

Now selected  image will display on the image box and path will be display on the ListBox.


Figure 3.


More Articles

© 2020 DotNetHeaven. All rights reserved.