[c#] Open file dialog and select a file using WPF controls and C#

I have a TextBox named textbox1 and a Button named button1. When I click on button1 I want to browse my files to search only for image files (type jpg, png, bmp...). And when I select an image file and click Ok in the file dialog I want the file directory to be written in the textbox1.text like this:

textbox1.Text = "C:\myfolder\myimage.jpg"

This question is related to c# wpf textbox openfiledialog

The answer is


var ofd = new Microsoft.Win32.OpenFileDialog() {Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"}; 
var result = ofd.ShowDialog();
if (result == false) return;
textBox1.Text = ofd.FileName;

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to wpf

Error: the entity type requires a primary key Reportviewer tool missing in visual studio 2017 RC Pass command parameter to method in ViewModel in WPF? Calling async method on button click Setting DataContext in XAML in WPF How to resolve this System.IO.FileNotFoundException System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll? Binding an Image in WPF MVVM How to bind DataTable to Datagrid Setting cursor at the end of any text of a textbox

Examples related to textbox

Add two numbers and display result in textbox with Javascript How to check if a text field is empty or not in swift Setting cursor at the end of any text of a textbox Press enter in textbox to and execute button command javascript getting my textbox to display a variable How can I set focus on an element in an HTML form using JavaScript? jQuery textbox change event doesn't fire until textbox loses focus? PHP: get the value of TEXTBOX then pass it to a VARIABLE How to clear a textbox once a button is clicked in WPF? Get current cursor position in a textbox

Examples related to openfiledialog

Open a workbook using FileDialog and manipulate it in Excel VBA How to get file path from OpenFileDialog and FolderBrowserDialog? Open File Dialog, One Filter for Multiple Excel Extensions? Reading a text file using OpenFileDialog in windows forms How to use OpenFileDialog to select a folder? Open file dialog and select a file using WPF controls and C# Quick and easy file dialog in Python? Obtaining only the filename when using OpenFileDialog property "FileName" Load a bitmap image into Windows Forms using open file dialog Multiple file extensions in OpenFileDialog