C# WPF Tutorial #11 - FolderBrowserDialog - How to use Winforms In WPF!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone this time we are learning how to use the folder browser dialog so this tutorial will be a little bit different than the others in that rather than creating something with WPF we are going to use something that winforms already has you don't often have to do this with WPF but occasionally it will save you time if winforms already has exactly what you want to get started with this tutorial all you need is a button with a click Handler so that we can eventually use it to fire our dialog the first thing I want to show you is a trap that people often get stuck in when trying to incorporate winforms controls and that is if you have the control folder browser dialog let's create a new one and then we'll call its show dialog method it's not working because it's in winforms not WPF so the packages are missing now when we've done this before a lot of times intellisense will tell us what we need and in this case it does but in this case it does not work now in the future they may fix this to where it does work but if you tell it to automatically fix using system.windows.forums and you click this it doesn't do anything usually it puts a using up here but if we do that manually system.windows.forms it's still not going to work because it does not know what it is because we are using WPF packages not win forms packages so intellisense is not able to fix this for you to allow us to use winforms what we need to do is go to our tutorial project right click it go to properties and then find the box that says enable Windows forms for this project tick that and save it and what that's going to do is if we right click our project again go to Open folder in file explorer right click our project file and edit it now you'll see a tag called use Windows forms and it will be true so we have used WPF true automatically with a WPF project now we have this as well and that's going to enable us to use packages from this UI framework also now if we go back to our code and do the same thing let our intellisense tell us what to do using system.windows.forms will actually work now and our class will resolve itself and our error will go away now before we jump into our dialog I want to show you trap number two that comes along with using winforms inside of WPF now that we have the winforms package available to us we have also made it possible to have a lot of ambiguity because winforms has a lot of the same things that WPF has like message box so if we try to say message box dot show like we did in a previous video it's going to tell us message box is an ambiguous reference between our winforms message box and our WPF message box so that now anytime we have this using present we can run into these issues and intellisense suggests that we either say system.windows or system.windows.forms to let it know which one we want to use but I highly suggest that instead of doing that you create an alias for Windows Form so that you have to use it and you never mistakenly use the wrong one to do that all you have to do is say using and then give it a variable name which I always say win forms equals so now win forms equals system.windows.forums so as you can see down here this doesn't know what it is anymore and to use it we have to say winforms dot so now anytime we want to call winform specifically we use our Alias and every other time it knows to use the WPF packages that way it keeps our code cleaner and we don't accidentally use winforms when we don't want to okay now that we have everything set up we actually already have the code to fire our folder browser dialog now before we fire this up the dialog object has properties similar to the file dialog that can help you tell your users what to do or where to go so the most useful one in this case for me is initial director story so that we don't start in my documents all the time but instead we can start where we tell it so I'm going to copy the full path of my solution file I'm going to paste it in and I'm going to remove the file name because this is not a file browser it is a folder browser so now instead of starting in my documents our dialog will open up to the folder that we specify so now we can finally fire our application up we can fire our dialog and what we get is a dialog that says select folder we have started in the folder that we have specified and you'll notice that the solution file is not present and that's because it does not show files in the folder picker so if we drill down we can go all the way down into our debug folder but once we get to the actual executable build it won't show any of that because this is the last folder in the chain so the user is only able to select a folder and then the options are select folder cancel or close so we will cancel that close that just like the file dialog we did before the folder dialog is modal so the execution will stop until this command is finished so here we can get the result into a winforms DOT dialog result so we can say winform dot dialog result equals and again if you did not use an alias here you would have to fully qualify it as system dot Windows dot forms dot dialogresult like that now that we have the results stored from our dialog we can use it to say if result equals equals some dialog result we can do something now I want to point out that sometimes these windows can be a little misleading if we fire our dialogue you see there's not an OK button there's a select folder a cancel and a close but if we go over here and we put a break point here go back to our window and select folder you can see that the select folder button actually results in ok so if you use dialogue result say dot yes it's never going to enter this block because this particular dialog returns okay when they hit select folder so be aware of that if you ever have any issues so if our dialog results in OK that means the user selected a folder or a folder was initially selected so then folder would be dialog.selected path so if we run this what we're going to get is the folder that we choose let's go with bin select folder into our string and it's going to be the full path so now you are free to do with your path selected as you wish and of course you could have an else here to handle if the user did not choose a path when they needed to that wraps up the basics of using the folder browser dialog and incorporating winforms controls into WPF next up we're going to switch gears again and do the list view control so thank you for watching everybody I really appreciate you feel free to ask any questions you may have happy coding and as always until next time take care
Info
Channel: Kampa Plays
Views: 12,695
Rating: undefined out of 5
Keywords: C#, WPF, folder picker, folder select, alias, WinForms
Id: Heq8qve1Vts
Channel Id: undefined
Length: 7min 16sec (436 seconds)
Published: Thu Jan 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.