Welcome to Ultra Developers.

the company that developes your success

Read Blog


Displaying an alert window in your windows forms application using C#

Displaying an alert window in your windows forms application using C#

Some developers that use Microsoft live Messenger see an alert window when somebody of their contacts comes online. While developing BIMSI OMS applications some customers asked me to display alert messages when an order added to them and require the user to take an action.

When I saw the alert window of the Windows Live Messenger, I have decided to make BIMSI OMS alert as the one of Live Messenger.

Live Messenger

Creating an alert window as the one of Live Messenger:

  1. Create a new windows application using Visual Studio 2005/2008 or use any windows application you have created.
  2. Add a new Windows Forms in the newly created project and name it AlertForm.
  3. In the constructor of the AlertForm add the following code to display the AlertForm in the location above the Taskbar clock section.
Rectangle screenRectangle = Screen.PrimaryScreen.WorkingArea;
this.Location = new Point(screenRectangle.Width - this.Width - 10,
screenRectangle.Height - this.Height - 10); 
  1. The above code simply gets the working area of the client primary monitor screen and set the location of the Alert Form to the location computed above.
  2. You can also add a label control to display a message to the user.
  3. You can also add sound to the Alert Form as follows:
    • Import the System.Media namespace in the alert form. Using the following statement using System.Media;
    • Declare a new SoundPlayer soundPlayer object.
    • Pass the file path of the .wav sound file to the constructor of the soundPlayer object as follow soundPlayer = new SoundPlayer(“.wav sound file path”);
    • The file passed to soundPlayer object must be a .wav file. If you pass a .mp3 or other audio files it will throw an exception.
    • After that call the Play method of the soundPlayer object as follows:
soundPlayer.Play();
  1. In the form closing call the Stop method of the soundPlayer object as follows:
soundPlayer.Stop();
  1. If you want to make the alert window closes after a period of time:
    • Add the System.Timers.Timer component to the AlertForm and name it CloseTimer.
    • Set its interval to the period you want the form to close after for example set it to 3000 milliseconds (3 seconds).
    • Implement the Timer Elapsed event and add the following code in that event
private void CloseTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    this.Close();
}

Now after the timer period is elapsed the AlertForm will close.

  1. If you want to display the Alert Window in another thread other than the main one you can use the powerfull BackgroundWorker Component that execute tasks in a Background thread with the following steps:
    • Add the BackgroundWorker Component from the Toolbox and name it NotifyBackgroundWorker.
    • Implement The NotifyBackgroundWorker DoWork Event and write the code that will show the AlertForm as follows:
AlertForm alertForm = new AlertForm();
alertForm.Show();
  1. To display the Alert window to the user you will need to follow these steps:
    • Write the following code in the event or methods in which you need to notify users.
    • In the following code, we check if the NotifyBackgroundWorker is not doing any work and then we call the RunWorkerAsync() method that executes the code written in the DoWork Event of the NotifyBackgroundWorker.
if (!NotifyBackgroundWorker.IsBusy)
{
    NotifyBackgroundWorker.RunWorkerAsync();
}

With these steps you have created an alter window that like the one of the Windows Live Messenger

Alert Window

Similar Posts