Introduction
Here is an article for displaying notification from statusbar icon. And when user will double click on that icon it will open a web application. And on right click it will display menu to Hide, Show alerts as well Exit the alerts. So we can say that here we are going to implement an application which will work together as window based as well web based application. Here the alert will be displayed from database. And when there will not any mess in database it will not display any notification. But when any mess will come in database it will pop up notification.
Here is a snipshots for the same.
When user right click on Icon.
Using the code
First we will create a window application.Name the window application as AlertMessControl. Now add one form name it as AlertMess.
Add below controls and set appropriate properties to controls:
Timer :
this.tmrAlert.Enabled = true;
this.tmrAlert.Interval = 2000;
this.tmrAlert.Tick += new System.EventHandler(this.tmrAlert_Tick);
Label :
this.lblStatus.Name = "lblStatus";
NotifyIcon :
this.alertNot.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
this.alertNot.BalloonTipText = "Task Arrived";
this.alertNot.BalloonTipTitle = "New Task";
this.alertNot.Text = "Test all task";
this.alertNot.Visible = true;
this.alertNot.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.alertNot_MouseDoubleClick);
Label :
this.lblTotal.AutoSize = true;
this.lblTotal.Name = "lblTotal";
this.lblTotal.Text = "0";
this.lblTotal.Visible = false;
Form Property :
this.ControlBox = false;
this.Name = "AlertMess";
this.Opacity = 0.01;
this.ShowInTaskbar = false;
this.Text = "AlertMess";
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Collections;
using System.Security;
using System.Security.Permissions;
namespace AlertMessControl
{
public partial class AlertMess : Form
{
public AlertMess(string)
{
InitializeComponent();
}
private void hide_Click(object Sender, EventArgs e)
{
try
{
lblStatus.Text = "H";
alertNot.BalloonTipText= "";
alertNot.Visible =false;
alertNot.Visible =true;
tmrAlert.Stop();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void show_Click(object Sender, EventArgs e)
{
try
{
lblStatus.Text = "S";
tmrAlert_Tick(Sender, e);
tmrAlert.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void exit_Click(object Sender, EventArgs e)
{
try
{
lblStatus.Text = "E";
Application.Exit();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void tmrAlert_Tick(object sender, EventArgs e)
{
GetAlerts();
}
private void GetAlerts()
{
try
{
if(lblStatus.Text == "H")
{
alertNot.BalloonTipText= "";
alertNot.Visible =false;
alertNot.Visible =true;
goto BreakProcess;
}
if(lblStatus.Text == "E")
{
alertNot.Visible =false;
goto BreakProcess;
}
int total =0;
SqlConnection con;
SqlDataAdapter adp;
con = new SqlConnection("Connection String");
con.Open();
adp = new SqlDataAdapter("Command to get alert messages", con);
DataSet ds = new DataSet();
adp.Fill(ds);
total = ds.Tables[0].Rows.Count;
if(Convert.ToInt32(lblTotal.Text) != total)
{
alertNot.Visible = false;
alertNot.Visible = true;
}
lblTotal.Text = total.ToString();
int i = 1;
if(total >0)
{
foreach(DataRow dr in ds.Tables[0].Rows)
{
alertNot.ShowBalloonTip(1,dr["Mess"].ToString() , ToolTipIcon.Info);
i++;
}
con.Close();
}
BreakProcess:
if(lblStatus.Text == "H")
lblStatus.Text = "H";
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void AlertMess_Load(object sender, EventArgs e)
{
try
{
ContextMenu ctm = new ContextMenu();
EventHandler ev1 = new EventHandler(hide_Click);
ctm.MenuItems.Add("Hide Me", ev1);
EventHandler ev2 = new EventHandler(show_Click);
ctm.MenuItems.Add("Show Me", ev2);
EventHandler ev3 = new EventHandler(exit_Click);
ctm.MenuItems.Add("Exit", ev3);
alertNot.ContextMenu = ctm;
GetAlerts();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void alertNot_MouseDoubleClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Task Searching......");
Process prs = Process.Start(<a href=");/">http:
}
}
}
<p>Now you check your application using this code. </p>
<h2>Points of Interest</h2>
<p>Using this code you will get a great feeling to have notification in statusbar.</p>
<p>Enjoy the coding.. </p>
Working as a Team Lead in MNC located at India.
In IT world, I have worked with Microsoft Technologies and always ready for R&D.
Worked with Website Development and Windows based applications as well.
New errors are really good and being happy to resolve those.....