From the course: Visual Studio Developer Tips

Use DialogResult

- [Instructor] The purpose of a dialog window is to gather user input while blocking the user from returning to the main application UI. These types of windows are usually called modal windows. In this tip, I'll look at what values are returned from a dialog window in a WPF application. The way this project is laid out is I have a MainWindow, which serves as the main window of the application, and it's here that I'll instantiate and show the other windows. The other two windows in this project are in this Views folder. There's one called SimpleWindow, which looks like this, has an OK and a Cancel button. And PrintWindow has a ListBox, a TextBox, and an OK and Cancel button. Start by looking at MainWindow, and I'll look at the code behind here. In WPF, any window can function as a dialog. Let me show you what I mean. Here on line 19, I'm instantiating an instance of SimpleWindow. On line 20, I'm showing that to the user. This is not a modal window because I use the Show method. Line 26 shows a method called ShowDialog. This also shows the window to the user, but in this case it's a modal window and I can get a result back from that dialog. Here's what this looks like running in the application. Click on the Show Normal button. This is the SimpleWindow. The user can move from SimpleWindow back to MainWindow and then back to MainWindow. They're not blocked from moving from window to window. Clicking on the Show Dialog button brings up the same window, which looks nearly identical. However, when I click on MainWindow, I'm blocked from returning. I can't return there until the user dismisses this window. This ShowDialog method returns some information to my code. When I hover over the method, it shows that it returns a bool question mark, which stands for a nullable of bool. That means that there are three possible results that I can get back when I show that dialog. I can get a true value, a false value, or a null value. So what we'll look at next is when we get each of those three values back. You don't want a null result, but you will get it in certain circumstances. Here's where you'll get it. I've instantiated SimpleWindow on line 35. And then instead of calling ShowDialog, I go directly to a property on the window called DialogResult and I say what do you have for me? Since I never showed the dialog to the user, this value will be null. The other way this can happen is like this. If I called Show instead of ShowDialog, now when I go to test for this value, it'll also be null. The preferred way of handling this is to go like this, I write code that looks like this. So I call ShowDialog. Remember this returns a nullable of bool. So now I'll store that result here in this variable. So rather than using DialogResult, I can just store the results directly in this variable. Now because I'm calling ShowDialog, this value will never be null. It will either be true or false. So what's the difference? Well, by default, you're always going to get back false. When you open a dialog and the user closes that dialog, the value will be false. The only way it'll be true is if you write some code somewhere in that other window that sets the value to true. So let's see how that's done. Go to SimpleWindow, go to the code behind. When the user clicks on the OK button, I set DialogResult equal to true. When the user clicks on the Cancel button, I set the DialogResult to false. Now remember I said this is the default, so in most of the cases I don't need to do this. But this is a good practice when the user clicks on Cancel to set it to false because the user could've been working in the dialog for a while, selected several options, and then changed their mind and clicks on the Cancel button to return. This just ensures that the value gets set to false. So here's what this looks like. Click on this bottom button here. So the dialog pops up. The user decides, no, I didn't want to, I really didn't want to be in here, so they click here. That sends a false result back. Do it again. User closes it using the context menu over here. They still get a false result back. Or they click on the Cancel button, for the third time, they're getting false. And now when they click on OK, you get true. So this is how you know in your code that the user closed the dialog in a way that you expect. So you would then write an if statement that tests whether you got a true value back and then proceed with whatever else actions you need to perform after getting the dialog result. At this point, we know how to close the dialog. In some cases, that might be all you need, or it could be that you want to get additional values from the dialog window. That's the topic for another day. The answer depends on many factors. For example, are you using a Model-View-ViewModel pattern with your windows? But let me show you one simple example before we go. In PrintWindow, remember that this has this TextBox here? So one way I can return a value other than just a true/false value is I can create custom properties on my window class. In WPF, these are typically done as dependency properties. But for this example, I'm just doing simple properties. Down here, I've got a NumberOfCopies property, and you can see that it's just returning the integer value from that TextBox. So now, over in MainWindow, I can show the dialog. Check the results are true. Did the user click the OK button? If so, then go to that property and retrieve the value, like this. Type in 12, click OK, get that result back. Do one more test, type in a value of four, and get that value back.

Contents