Implementing Alerts in C# - Oracle Forms to .NET Migration

In Oracle Forms, an alert is a modal window that displays a message (similar to MessageBox in Windows Forms/WPF).

There are three styles of alerts: Stop, Caution and Note that denote a different level of message severity and have a different icon in the alert window.

When an alert is shown, the user must respond by selecting one of the predefined buttons, and then the window is closed.

Creating Alerts in Oracle Forms

You can create custom alerts for each form. Actually alerts are one of the forms components along with data blocks, triggers, canvases, LOVs etc.

When you create an alert you specify its name, dialog window title, message text and buttons. You can specify to show up to 3 buttons, and each button may have any text on it.

Examples of alert windows:

  • Alert with Stop Style

  • Alert with Caution Style

  • Alert with Note Style

Displaying Alerts in Oracle Forms

You can execute SHOW_ALERT built-in function from a trigger or user-defined subprogram to display an alert. You can specify alert ID or alert name as the parameter to SHOW_ALERT.

To show alert by specifying its name:

    DECLARE
        ret NUMBER;
    BEGIN
       ret := SHOW_ALERT('SHOW_ERROR');
    END;

Oracle Forms allows you to change the alert title and message text using SET_ALERT_PROPERTY built-in procedure.

    DECLARE
        ret NUMBER;
    BEGIN
       SET_ALERT_PROPERTY('SHOW_ERROR', ALERT_MESSAGE_TEXT, 'Error during statement execution'); 
       SET_ALERT_PROPERTY('SHOW_ERROR', TITLE, 'Critical Error'); 
 
       ret := SHOW_ALERT('SHOW_ERROR');
    END;

You can also specify alert ID in SET_ALERT_PROPERTY and SHOW_ALERT functions. To get the alert ID use FIND_ALERT built-in function:

    DECLARE
        id ALERT;
        ret NUMBER;
    BEGIN
       id := FIND_ALERT('SHOW_ERROR');
 
       SET_ALERT_PROPERTY(id, ALERT_MESSAGE_TEXT, 'Error during statement execution'); 
       SET_ALERT_PROPERTY(id, TITLE, 'Critical Error'); 
 
       ret := SHOW_ALERT(id);
    END;

SHOW_ALERT returns a numeric constant ALERT_BUTTON1, ALERT_BUTTON2 or ALERT_BUTTON3 depending which button was selected. You can specify button mappings when you design the alert.

C# .NET Implementation

Both Windows Forms and Windows Presentation Foundation (WPF) offer MessageBox.Show method that can be used to show message dialogs. When you invoke this method, you can specify the title, message text, dialog style, and which buttons to show.

For example, to show an error message with single Ok button, you can execute:

   /* Windows Presentation Foundation (WPF) Method */
   System.Windows.MessageBox.Show("Error processing the command.", "Error", 
                   System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);

It will show the following dialog:

Alert and AlertManager Classes

In some scenarios, especially when the same message boxes are shown from many different parts of application it may be convenient to use Oracle Forms approach and put all message information into one place.

You can use Alert and AlertManager classes:

   using System;
   using System.Collections.Generic;
   using System.Windows;
 
   namespace ObjectLibrary
   {
       class Alert
       {
           public string Name { get; set; }
           public string Title { get; set; }
           public string Text { get; set; }
           public MessageBoxButton Buttons { get; set; }
           public MessageBoxImage Icon { get; set; }
       }
 
       class AlertManager
       {
           // List of alerts
           private static Dictionary<string, Alert> _alerts = new Dictionary<string, Alert>();
 
           public static Dictionary<string, Alert> Alerts { get { return _alerts; } }
 
           // Add a new alert
           public static void Add(string name, string title, string text, MessageBoxButton buttons,
               MessageBoxImage icon)
           {
               _alerts.Add(name, new Alert()
               {
                   Name = name, Title = title, Text = text, Buttons = buttons, Icon = icon
               });
           }
 
           // Find alert by name
           public static Alert Find(string name)
           {
               Alert alert = null;
 
               if (name != null)
               {
                   try
                   {
                       alert = Alerts[name];
                   }
                   catch (KeyNotFoundException) { }
               }
 
               return alert;
           }
 
           // Show alert by name
           public static MessageBoxResult Show(string name)
           {
               return Show(Find(name));
           }
 
           // Show alert by id
           public static MessageBoxResult Show(Alert alert)
           {
               if( alert != null)
                   return MessageBox.Show(alert.Text, alert.Title, alert.Buttons, alert.Icon);
 
               return MessageBoxResult.Cancel;
           }
       }
   }

Before you can display alerts, you have to add them as follows:

    AlertManager.Add("SHOW_ERROR", "Error", "Error processing the command.", MessageBoxButton.OK,
                MessageBoxImage.Error);

Then you can display the alert by calling:

   // Show alert by name
   AlertManager.Show("SHOW_ERROR");

You can also modify the alert title and message:

   // Change the title and message text
   Alert alert = AlertManager.Find("SHOW_ERROR");
 
   if (alert != null)
   {
       alert.Title = "Critical error";
       alert.Text = "An error occurred.";
       AlertManager.Show(alert);
   }

Download Source Code

You can download the source code for the demo application that also includes Alert and AlerManager classes at Alert Demo (6 Kb)

Migration Resources

You could leave a comment if you were logged in.