WPF and Silverlight Edition Basic Library / Windows / Windows Features / Modal and Modeless Windows
Modal and Modeless Windows

Dialog boxes are commonly used in applications to retrieve input from the user. In some applications a dialog box is used to prompt the user for input and once the application retrieves the input the dialog box is automatically closed or destroyed.

On the other hand, some applications use dialog boxes to display information while the user works in other windows. For example, when you check spelling in Microsoft Word a dialog box remains open so you can go through and edit your text in the document while the spell checker looks for the next misspelled word. To support the different ways applications use dialog boxes, C1Window supports two different types of dialog windows: modal and modeless dialog windows.

A modal dialog window is a child window that must be closed before the user can continue working on the current application. Typically modal dialog windows either take control of the entire system or application displaying them until they are closed. For example, you can use a modal dialog window to retrieve login information from a user before the user can continue working on an application. Modal windows are useful in presenting important information or requiring user interaction.

You can show the C1Window control as a modal dialog box using the ShowModal method:

Visual Basic
Copy Code
'The Show method is seen here in an If...Then...Else statement.
If showModal Then
        wnd.ShowModal()
    Else
        wnd.Show()
    End If

 

C#
Copy Code
//The Show method is seen here in an if...else statement.
if (showModal)
      wnd.ShowModal();
    else
      wnd.Show();

Setting the Modal Background Color

You can customize the color of your modal background using the ModalBackground property:

  1. Once you complete the Windows Quick Start, switch to Code view and locate the code for the Button2_Click event.
  2. Edit the Click event so that it resembles the following:
Visual Basic
Copy Code
Private Sub ShowDialog(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim window = New C1Window()
    window.Content = New MyWindow()
    window.CenterOnScreen()
    Dim bgcol As New SolidColorBrush()
    bgcol.Color = Color.FromArgb(150, 255, 0, 0)
    window.ModalBackground = bgcol
    window.ShowModal()
End Sub

 

C#
Copy Code
void ShowDialog(object sender, RoutedEventArgs e)
{
    var window = new C1Window();
    window.Content = new MyWindow();
    window.CenterOnScreen();
    SolidColorBrush bgcol = new SolidColorBrush();
    bgcol.Color = Color.FromArgb(150, 255, 0, 0);
    window.ModalBackground = bgcol;
    window.ShowModal();
}
  1. When you run your application now and click the Open a modal window. button, the background will be red: