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.

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:

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

opd1.gif

Figure 1.

XAML code

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

    <Grid>

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

    </Grid>

</Window>

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"

        myDialog.ShowDialog()

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

        ListBox1.Items.Add(myDialog.FileName)

    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.

opd2.gif

Figure 2.

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

opd3.gif

Figure 3.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.