Dialog Boxes in WPF

In this article we will discuss about Dialog Boxes in WPF. First we use an example of a MessageBox in WPF. In order to create a MessageBox use the following steps.

MessageBox

Step1: First we take a Button and in our WPF page to show the MessageBox.

<Button Height="23" Margin="115,87,44,0" Name="ShowMessageBox" VerticalAlignment="Top" Click="ShowMessageBox_Click">Show Message Box</Button>

Step2: Then we write the following code in the .cs page:

private void ShowMessageBox_Click(object sender, RoutedEventArgs e)
        {
string msgtext = "Do you want to save Anything In this Document?";
            string txt = "My Title";
            MessageBoxButton button = MessageBoxButton.YesNoCancel;
            MessageBox.Show(msgtext, txt, button);
            MessageBoxResult result = MessageBox.Show(msgtext, txt, button);   
       }

The following output will be appear:

p1.png

Now we take an another example in it. In this Case, we want to show the messages on the click of the MessageBox Buttons (Yes, No, Cancel):

Step1: First we use a TextBox to show the result when the Buttons are clicked.

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,48,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />

Step 2: Now we write the following code in the ShowMessageBox Button.

private void ShowMessageBox_Click(object sender, RoutedEventArgs e)
         {
            string msgtext = "Do you want to save Anything In this Document?";
            string txt = "My Title";
            MessageBoxButton button = MessageBoxButton.YesNoCancel;
            MessageBox.Show(msgtext, txt, button);
            MessageBoxResult result = MessageBox.Show(msgtext, txt, button);
             switch
(result)
            {
                case MessageBoxResult.Yes:
                     textBox1.Text = "Yes";
                    break;
                case MessageBoxResult.No:
                     textBox1.Text = "No";
                     break;
                case MessageBoxResult.Cancel:
                      textBox1.Text = "Cancel";
                    break;
            }

        }

So when we click on the Ok Button, the output will be:

p2.png

OpenFileDialogBox:

Now we take an example of an OpenFileDialogBox, in which when we click on the Button; the OpenFileDialogBox will be appear.

Step1: For this first we take a Button.

<Button Margin="10,114,138,125" Name="OpenFileDialogBox" Click="OpenFileDialogBox_Click">Open File Dialog Box</Button>

Step2: Then we write the following code in the .cs page:

private void OpenFileDialogBox_Click(object sender, RoutedEventArgs e)
        {
             Microsoft.Win32.OpenFileDialog dl1 = new Microsoft.Win32.OpenFileDialog();
            dl1.FileName = "MYFileSave";
            dl1.DefaultExt = ".txt";
            dl1.Filter = "Text documents (.txt)|*.txt";            
            Nullable<bool> result = dl1.ShowDialog();
            if (result == true)
            {          
                string filename = dl1.FileName;

            }

        }

In this code:

dl1.FileName = "MYFileSave";
dl1.DefaultExt = ".txt";

This is the default Name and Extension of the file.

OUTPUT

p3.png

SaveFileDialogBox

Now we take an example of the SaveFileDialogBox in which, when we click on the Button, the SaveFileDialogBox will appear.

Step1: For this first we take a Button.

<Button Margin="89,0,59,96" Name="SaveFileDialogBox" Click="SaveFileDialogBox_Click" Height="23" VerticalAlignment="Bottom">Save File Dialog Box</Button>

Step2: Then we write the following code in the .cs page:

private void SaveFileDialogBox_Click(object sender, RoutedEventArgs e)

        {
            Microsoft.Win32.SaveFileDialog dl1 = new Microsoft.Win32.SaveFileDialog();
            dl1.FileName = "MYFileSave";
            dl1.DefaultExt = ".txt";
            dl1.Filter = "Text documents (.txt)|*.txt";
            Nullable<bool> result = dl1.ShowDialog();
            if (result == true)
            {
                string filename = dl1.FileName;

            }

        }

OUTPUT

p4.png

PrintFileDialogBox

Now we take an example of PrintFileDialogBox, in which when we click on the Button The PrintFileDialogBox will be appear.

Step1: For this First we take a Button.

<Button Margin="0,114,0,125" Name="PritFileDialogBox" Click="PrintFileDialogBox_Click" HorizontalAlignment="Right" Width="130">Print File Dialog Box</Button>

Step2: Then we write the following code in the .cs page.

private void PrintFileDialogBox_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.PrintDialog dl1 = new System.Windows.Controls.PrintDialog();
            dl1.PageRangeSelection = PageRangeSelection.AllPages;
            dl1.UserPageRangeEnabled = true;
            Nullable<bool> result = dl1.ShowDialog();
            if (result == true)
            {           

            }
         }


OUTPUT

p5.png