Javatpoint Logo
Javatpoint Logo

WPF Dialog Box

The standalone application contains the main window where they will show the data of the application. Standalone application will acquire the functionality of the process which will show the data through the user interface mechanism like menu bars, toolbars, and status bars. The non-trivial application can also display the additional windows to do the below tasks:

  1. We will use windows to show the specific information to the users.
  2. With the help of the windows of the non-trivial application, we can show the information of the users.
  3. We will use the application for the purpose of showing and gathering the information from the users.

All these windows which we used for the different purpose are known as a dialog box. There are two types of dialog boxes.

  1. Modal
  2. Modeless

Now we will explain both the dialog box.

  1. Modal: The modal dialog box will appear when there is the need of the additional data. The need of the modal dialog box is to collect the data. When the dialog box is open then this will prevent us to activate the other window in the application. In most of the cases, modal dialog box allows us to give the signal when we finished the task. We will show the signal by pressing the button of the dialog box that can be OK or the Cancel button. When we press the OK button, we want to function the entered data and want to continue the processing along with that data. When we click on the Cancel button, cancel button will show that we want to stop the function from executing together. The most common example of the Modal dialog box is Open, Save, and print data.
  2. Modeless: The difference between the Modal and Modeless dialog box is that the modeless dialog box does not prevent us to activate the other windows when we try to open the other window. Now we will take an example if we want to find the word's occurrence in the document. Now the main window will open the dialog box to find out what we want to search. As we know, searching for the word doesn't affect the document edition. That is why the dialog box does not need to be modal. The modeless dialog box provides a Close button to close the dialog box and gives us additional buttons to execute the specific function, such as the Find Next button, to find the next matching word.

With the help of the WPF, we can create different types of the dialog boxes which include the message boxes, dialog boxes, and the custom dialog boxes.

Now we will explain different types of dialog boxes.

Message Boxes: A message box is a dialog box that we can use to show the textual information and allow the users to make the decisions with the help of the buttons.

We can create the Message Box by using the Message Box class.

The below figure shows us the message box, which shows the textual information, asks the question from us, and provides three buttons to answer with the help of the buttons.

WPF Dialog Box

2. Common Dialog Boxes: Windows implements the different types of reusable dialog boxes. These common dialog boxes are common to all the applications. When we want to open a file, this will show us the dialog box, when we try to save the file and wants to print then the common dialog box will appear. Now the common dialog box will expose the dialog box in the form of the managed class and we can also use those classes in the standalone applications.

To open the file dialog box, we use the file opening functionality to retrieve the name of the file which we want to open like as shown in the below screenshot:

WPF Dialog Box

To open the common file dialog box, we used the OpenFileDialog class and located in the Microsoft.Win32 namespace.

Custom Dialog Boxes

Custom Dialog boxes are very useful and we can use these dialog box when they are needed. Custom Dialog Boxes doesn't support the domain-specific dialog boxes. In this case, we will create our dialog box.

Now we will create the custom Dialog box.

To understand the concept of the dialog box, in the first we will create a new WPF project whose name is WPFDialogBox.

Now we will take one text box and one button. When we click on the button, this will open another dialog box that contains the Yes, No, and Cancel button. When we click on any button, this will show us a new message.

When we click on any button, that button shows us a message and close itself. The button shows us information when we click on them.

To add the textbox and a button on the page we will write the below XAML code:

MainWindow.XAML

Now we will write the C# code on the generated event after clicking on the button.

MainWindow.XAML.CS

The output of the above code will look like as shown in the below screenshot:

Output

WPF Dialog Box

In the above screenshot, when we click on the "Yes" button, this shows the content in the above textbox.


Next TopicWPF Context Menu





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA