Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Sticky Notepad v2

Add Comment
 

 
Download File SDK
stickynote2.zip (19kb) Beta2

Introduction
I am a Java programmer and hence I wanted to experiment with writing a program that would run from the "System Tray" of Windows (that's where you have the date displayed) since that's not possible with pure Java!
So I have built a application which uses "NotifyIcon" component of Windows Forms to show the run the program from the System Tray. This example has been updated for .NET SDK Beta2. It also requires Visual Studio.NET to build the example.
The 'TrayIcon' class in Beta1 has changed to 'NotifyIcon' in Beta2.

Requirements
1) .NET SDK beta2 (Note: This example might not run on future versions of the SDK) 
2) Visual Studio.NET Beta2

Point To Remember
Don't forget to copy the file "sticky.txt" to the directory in which you will be running the program from. 

Screen Shot

Figure 1: Stick Notepad

Code
1) sticky.cs :- The Sticky Notepad

namespace MasterCSharp.WebSite.Saurabh.WindowsForms
{
  //Import the necessary Assemblies
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.IO ;

  /// <summary>
  ///    The class to demonstrate a Sticky Pad
  /// </summary>
  public class sticky : System.Windows.Forms.Form
  {
    /// <summary>
    ///    Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components;
    private System.Windows.Forms.MenuItem menuItem4;
    private System.Windows.Forms.MenuItem menuItem3;
    private System.Windows.Forms.MenuItem menuItem2;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    //NotifyIcon is used to place the Application in the System Tray
    private System.Windows.Forms.NotifyIcon StickyNote;
    private System.Windows.Forms.RichTextBox tbox;

    /// <summary>
    ///	The Constructor
    ///	We Call 2 methods here
    ///	InitilizeComponent - to Initialize the Form
    ///	read - to read the data from the text file
    /// </summary>
    public sticky()
    {
      InitializeComponent();
      read();
    }

    /// <summary>
    ///    Clean up any resources being used.
    /// </summary>
    public override void Dispose()
    {
      base.Dispose();
      components.Dispose();
    }

    /// <summary>
    ///    Required method for Designer support - do not modify
    ///    the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.Resources.ResourceManager resources =
           new System.Resources.ResourceManager(typeof(sticky));
      this.menuItem3 = new System.Windows.Forms.MenuItem();
      this.menuItem1 = new System.Windows.Forms.MenuItem();
      this.menuItem2 = new System.Windows.Forms.MenuItem();
      this.menuItem4 = new System.Windows.Forms.MenuItem();
      this.tbox = new System.Windows.Forms.RichTextBox();
      this.contextMenu1 = new System.Windows.Forms.ContextMenu();
      this.StickyNote = new System.Windows.Forms.NotifyIcon(this.components);
      this.SuspendLayout();
      //
      // menuItem3
      // 
      this.menuItem3.Index = 2;
      this.menuItem3.Text = "Help";
      this.menuItem3.Click += new System.EventHandler(this.helpme);
      //
      // menuItem1
      // 
      this.menuItem1.Index = 0;
      this.menuItem1.Text = "Show";
      this.menuItem1.Click += new System.EventHandler(this.maximise);
      //
      // menuItem2
      // 
      this.menuItem2.Index = 1;
      this.menuItem2.Text = "Hide";
      this.menuItem2.Click += new System.EventHandler(this.minimise);
      //
      // menuItem4
      // 
      this.menuItem4.Index = 3;
      this.menuItem4.Text = "Exit";
      this.menuItem4.Click += new System.EventHandler(this.Exit);
      //
      // tbox
      // 
      this.tbox.AutoWordSelection = true;
      this.tbox.BackColor = System.Drawing.Color.Orange;
      this.tbox.CausesValidation = false;
      this.tbox.ContextMenu = this.contextMenu1;
      this.tbox.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tbox.Name = "tbox";
      this.tbox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
      this.tbox.Size = new System.Drawing.Size(202, 179);
      this.tbox.TabIndex = 1;
      this.tbox.TabStop = false;
      this.tbox.Text = "";
      //
      // contextMenu1
      // 
      this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    						this.menuItem1,
						this.menuItem2,
						this.menuItem3,
						this.menuItem4});
      //
      // StickyNote
      // 
      this.StickyNote.ContextMenu = this.contextMenu1;
      this.StickyNote.Icon =
		((System.Drawing.Icon)(resources.GetObject("StickyNote.Icon")));
      this.StickyNote.Text = "StickyNote";
      this.StickyNote.Visible = true;
      //
      // sticky
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.BackColor = System.Drawing.Color.Orange;
      this.CausesValidation = false;
      this.ClientSize = new System.Drawing.Size(202, 179);
      this.ContextMenu = this.contextMenu1;
      this.ControlBox = false;
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.tbox});
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.MaximizeBox = false;
      this.Name = "sticky";
      this.ShowInTaskbar = false;
      this.Text = "Sticky Note, By Saurabh Nandu";
      this.TopMost = true;
      this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
      this.ResumeLayout(false);
   }

    /// <summary>
    ///	This function is called when "Hide"
    ///	is selected from the context menu
    /// </summary>
    protected void minimise (object sender, System.EventArgs e)
    {
      if(this.Visible)
      {
         //Hide the Application
	this.Hide();
      }
    }

    /// <summary>
    ///	This method is called when "Show"
    ///	is selected from the context Menu
    /// </summary>
    protected void maximise (object sender, System.EventArgs e)
    {
      //Maximize the Window
      //Why??? since we are initially setting the Forms
      //State to "Minimized" in the constructor
      this.WindowState = System.Windows.Forms.FormWindowState.Normal;
      if(!this.Visible)
      {
	//If the Window is hidden
	//Show it
	this.Show();
       }
     }
     /// <summary>
     ///	Method called when "Help" button clicked
     /// </summary>
     protected void helpme (object sender, System.EventArgs e)
     {
       MessageBox.Show("Sticky Note Made by Saurabh Nandu,
    		saurabh@mastercsharp.com") ;
     }

     /// <summary>
     ///	Called when "Exit" is selected from the context menu.
     /// </summary>
     protected void Exit (object sender, System.EventArgs e)
     {
       this.save();
       //Call the Dispose Method
       this.Close();
     }
    /// <summary>
    ///	It saves the Data to the Text File
    /// </summary>
    protected void save()
    {
      //Open a File Stream
      FileStream fout = new FileStream("sticky.txt", FileMode.Truncate,
		FileAccess.Write, FileShare.Read) ;
      //Get a StreamWriter , since its easy to write string with it.
      StreamWriter sw = new StreamWriter(fout) ;
      //Write the contents for the RichTextBox
      sw.Write(tbox.Text);
      sw.Flush();
      //Close the Streams
      sw.Close();
    }

    /// <summary>
    ///	This method is called from the constructor
    ///	It read the text from the Text file
    ///	and displays it in the RichTextBox
    /// </summary>
    protected void read()
    {
       //Open the streams to the file
       FileStream fin = new FileStream("sticky.txt", FileMode.Open,
			FileAccess.Read, FileShare.ReadWrite) ;
       StreamReader tr = new StreamReader(fin) ;
       //Read the data, I use "ReadToEnd" cause with it you
       //can read the whole file in one shot !
       tbox.Text = tr.ReadToEnd();
       //Close the streams
       tr.Close();
       fin.Close();
    }
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    public static void Main(string[] args)
    {
      Application.Run(new sticky());
    }
  }
}

Comments

Add Comment