C# – Simple Desktop Notification With Two Return Codes

email me

This creates a custom notification with two return codes. These return codes can be recorded using desktop management software, such as LANDesk or SCCM. Thus giving you the ability to notify and instantly collect responses from end-users; it provides a method to communicate with an entire fleet of machines.

It works by assigning exit values of 100 and 200 to answer buttons, whatever the user selects becomes the output value, which shows up in the description or return code column of most desktop management software. In SCCM, the value appears in description, in custom queries, and in SSRS reports.

Full Project

Compiled in Visual 2017, and tested under Windows 10.


Output

Yes = 100
No = 200


Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Rectangle r = Screen.PrimaryScreen.WorkingArea;
StartPosition = FormStartPosition.Manual;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, Screen.PrimaryScreen.WorkingArea.Height - Height);
}

private void Button1_Click(object sender, EventArgs e)
{
Environment.Exit(exitCode: 100);
}

private void Button2_Click(object sender, EventArgs e)
{
Environment.Exit(exitCode: 200);
}
}
}


Form1.Designer.cs

namespace WindowsFormsApp6
{
partial class Form1
{
///

<summary>
/// Required designer variable.
/// </summary>private System.ComponentModel.IContainer components = null;

///

<summary>
/// Clean up any resources being used.
/// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing  (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///

<summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>private void InitializeComponent()
{
Form1 form1 = this;
form1.button1 = new System.Windows.Forms.Button();
form1.label1 = new System.Windows.Forms.Label();
form1.button2 = new System.Windows.Forms.Button();
form1.pictureBox1 = new System.Windows.Forms.PictureBox();
form1.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)form1.pictureBox1).BeginInit();
form1.SuspendLayout();
//
// button1
//
form1.button1.BackColor = System.Drawing.SystemColors.ButtonFace;
form1.button1.ForeColor = System.Drawing.SystemColors.InfoText;
form1.button1.Location = new System.Drawing.Point(76, 68);
form1.button1.Margin = new System.Windows.Forms.Padding(6);
form1.button1.Name = "button1";
form1.button1.Size = new System.Drawing.Size(88, 39);
form1.button1.TabIndex = 0;
form1.button1.Text = "Yes";
form1.button1.UseVisualStyleBackColor = false;
form1.button1.Click += new System.EventHandler(form1.Button1_Click);
//
// label1
//
form1.label1.AutoSize = true;
form1.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
form1.label1.ForeColor = System.Drawing.SystemColors.ControlText;
form1.label1.Location = new System.Drawing.Point(54, 22);
form1.label1.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
form1.label1.Name = "label1";
form1.label1.Size = new System.Drawing.Size(243, 20);
form1.label1.TabIndex = 2;
form1.label1.Text = "Is your Chrome browser working?";
//
// button2
//
form1.button2.BackColor = System.Drawing.SystemColors.ButtonFace;
form1.button2.ForeColor = System.Drawing.SystemColors.InfoText;
form1.button2.Location = new System.Drawing.Point(184, 68);
form1.button2.Margin = new System.Windows.Forms.Padding(6);
form1.button2.Name = "button2";
form1.button2.Size = new System.Drawing.Size(88, 39);
form1.button2.TabIndex = 6;
form1.button2.Text = "No";
form1.button2.UseVisualStyleBackColor = false;
form1.button2.Click += new System.EventHandler(form1.Button2_Click);
//
// pictureBox1
//
form1.pictureBox1.Image = global::WindowsFormsApp6.Properties.Resources.google;
form1.pictureBox1.Location = new System.Drawing.Point(117, 130);
form1.pictureBox1.Name = "pictureBox1";
form1.pictureBox1.Size = new System.Drawing.Size(108, 101);
form1.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
form1.pictureBox1.TabIndex = 5;
form1.pictureBox1.TabStop = false;
//
// label2
//
form1.label2.AutoSize = true;
form1.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
form1.label2.ForeColor = System.Drawing.SystemColors.ControlText;
form1.label2.Location = new System.Drawing.Point(108, 246);
form1.label2.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
form1.label2.Name = "label2";
form1.label2.Size = new System.Drawing.Size(127, 13);
form1.label2.TabIndex = 7;
form1.label2.Text = "Commlink Industries 2019";
//
// Form1
//
form1.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
form1.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
form1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
form1.ClientSize = new System.Drawing.Size(340, 274);
form1.ControlBox = false;
form1.Controls.Add(form1.label2);
form1.Controls.Add(form1.button2);
form1.Controls.Add(form1.pictureBox1);
form1.Controls.Add(form1.button1);
form1.Controls.Add(form1.label1);
form1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
form1.ForeColor = System.Drawing.SystemColors.Window;
form1.Margin = new System.Windows.Forms.Padding(6);
form1.MaximizeBox = false;
form1.MinimizeBox = false;
form1.Name = "Form1";
form1.Opacity = 0.8D;
form1.ShowIcon = false;
form1.ShowInTaskbar = false;
form1.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
form1.Text = "Notification";
form1.TopMost = true;
((System.ComponentModel.ISupportInitialize)form1.pictureBox1).EndInit();
form1.ResumeLayout(false);
form1.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label2;
}
}


Program.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
static class Program
{
///

<summary>
/// The main entry point for the application.
/// </summary>[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

 

Notes

SCCM WQL Query

select sys.Name, sys.LastLogonUserName, offer.LastExecutionResult, sys.ResourceDomainORWorkgroup from sms_r_system as sys inner join SMS_ClientAdvertisementStatus as offer on sys.ResourceID=offer.ResourceID WHERE AdvertisementID = ‘ABC10589’ and (LastStateName = “Failed”)
SCCM Reporting

 

Launch a website with button

// the button event
private void Button1_Click(object sender, EventArgs e)
{
// launch the webpage
Process.Start("https://google.com");

// the return code, which is recorded in SCCM
Environment.Exit(exitCode: 100);
}

 

Handling a timed, async event

// the button event
private async void Button2_Click_1(object sender, EventArgs e)
{

// minimizes to taskbar
WindowState = FormWindowState.Minimized;

// waits for delay before continuing
await Task.Delay(30000);

// do this after delay
WindowState = FormWindowState.Normal;
}

 

Do not show if specific process is running

public async void CheckProcess()
{
Process[] pname = Process.GetProcessesByName("notepad"); //presentationsettings
if (pname.Length == 0)
WindowState = FormWindowState.Normal;
else
WindowState = FormWindowState.Minimized;
await Task.Delay(10000);      // 10 sec
//await Task.Delay(3600000);  // 1 hour
CheckProcess();
}

 

More Advanced CheckProcess

This will check for specific running apps. If the apps are running, hide notification.

public async void CheckProcess()
{
do
{
bool ScanTitles = false;
bool sTitles = ScanTitles;

Process[] processlist = Process.GetProcesses();
foreach (Process process in processlist)
{
if (!String.IsNullOrEmpty(process.MainWindowTitle))
{
string str = process.MainWindowTitle;
if (str.Contains("Meet")) { sTitles = true; } // Google Hangouts
}
}

ScanTitles = sTitles;
//MessageBox.Show(ScanTitles.ToString());

Process[] pname1 = Process.GetProcessesByName("presentationsettings");  // presentationsettings(presentation mode)
Process[] pname2 = Process.GetProcessesByName("POWERPNT"); // powerpoint
if (pname1.Length == 0  pname2.Length == 0  ScanTitles == false)
{
Show();
}
else
Hide();
await Task.Delay(1000);      // 1 sec

} while (true);

}

 

How I got the Notify Icon to work

Form1.Designer.cs

// notifyIcon1
//
this.notifyIcon1.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
this.notifyIcon1.BalloonTipText = "Technology Notification";
this.notifyIcon1.BalloonTipTitle = "Notification";
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "You have a notification!";
this.notifyIcon1.Visible = true;
this.notifyIcon1.Click += new System.EventHandler(this.NotifyIcon1_Click);

Form1.cs

notifyIcon1.Visible = true;

The Event

private void NotifyIcon1_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible = false;
Show();
WindowState = FormWindowState.Normal;
}