OpenFileDialog WPF in VB.NET

This article shows the OpenFileDialog to select a file in VB.NET.
  • 3475
 

This article shows the OpenFileDialog to select a file in VB.NET.

OpenFileDialog

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.

Filter

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:

Creating one Button and one Image control in xaml.

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

The window form looks like this. 

ofd1.gif

Figure1.gif

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

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

        myDialog.ShowDialog()

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

    End Sub

Now run the application and test it.

ofd2.gif

Figure2.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.