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

 


Sticky Notepad v2

Add Comment
 

 
<div align="center"> <table border="0" width="70%" class="outline"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"><a class="wbox" href="../../file/stickynote2.zip">stickynote2.zip</a> (19kb)</td> <td width="50%" class="outline">Beta2</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> I am a Java programmer and hence I wanted to experiment with writing a program that would run from the &quot;System Tray&quot; of Windows (that's where you have the date displayed) since that's not possible with pure Java!<br> So I have built a application which uses &quot;NotifyIcon&quot; component of Windows Forms to show the run the program from the System Tray.&nbsp;This example has been updated for .NET SDK Beta2. It also requires Visual Studio.NET to build the example.<br> The 'TrayIcon' class in Beta1 has changed to 'NotifyIcon' in Beta2.</p> <p><span class=wboxheado>Requirements</span><br> 1) .NET SDK beta2 (Note: This example might not run on future versions of the SDK)&nbsp;<br> 2) Visual Studio.NET Beta2</p> <p><span class=wboxheado>Point To Remember</span><br> Don't forget to copy the file &quot;sticky.txt&quot; to the directory in which you will be running the program from.&nbsp;</p> <p><span class=wboxheado>Screen Shot</span><br> <img border="0" src="../../img/stickynotepad.gif" width="207" height="214"><br> Figure 1: Stick Notepad</p> <p><span class=wboxheado>Code</span><br> 1) <i><b>sticky.cs</b> :- The Sticky Notepad</i></p> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>namespace MasterCSharp.WebSite.Saurabh.WindowsForms { <span class=cmt>//Import the necessary Assemblies</span> using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.IO ; <span class=cmt>/// &lt;summary&gt; /// The class to demonstrate a Sticky Pad /// &lt;/summary&gt;</span> public class sticky : System.Windows.Forms.Form { <span class=cmt>/// &lt;summary&gt; /// Required designer variable. /// &lt;/summary&gt;</span> 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; <span class=cmt>//NotifyIcon is used to place the Application in the System Tray</span> private System.Windows.Forms.NotifyIcon StickyNote; private System.Windows.Forms.RichTextBox tbox; <span class=cmt> /// &lt;summary&gt; /// The Constructor /// We Call 2 methods here /// InitilizeComponent - to Initialize the Form /// read - to read the data from the text file /// &lt;/summary&gt;</span> public sticky() { InitializeComponent(); read(); } <span class=cmt>/// &lt;summary&gt; /// Clean up any resources being used. /// &lt;/summary&gt;</span> public override void Dispose() { base.Dispose(); components.Dispose(); } <span class=cmt>/// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// &lt;/summary&gt;</span> 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(); <span class=cmt>// // menuItem3 // </span> this.menuItem3.Index = 2; this.menuItem3.Text = &quot;Help&quot;; this.menuItem3.Click += new System.EventHandler(this.helpme); <span class=cmt>// // menuItem1 // </span> this.menuItem1.Index = 0; this.menuItem1.Text = &quot;Show&quot;; this.menuItem1.Click += new System.EventHandler(this.maximise); <span class=cmt>// // menuItem2 // </span> this.menuItem2.Index = 1; this.menuItem2.Text = &quot;Hide&quot;; this.menuItem2.Click += new System.EventHandler(this.minimise); <span class=cmt>// // menuItem4 // </span> this.menuItem4.Index = 3; this.menuItem4.Text = &quot;Exit&quot;; this.menuItem4.Click += new System.EventHandler(this.Exit); <span class=cmt>// // tbox // </span> 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 = &quot;tbox&quot;; 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 = &quot;&quot;; <span class=cmt>// // contextMenu1 // </span> this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem2, this.menuItem3, this.menuItem4}); <span class=cmt>// // StickyNote // </span> this.StickyNote.ContextMenu = this.contextMenu1; this.StickyNote.Icon = ((System.Drawing.Icon)(resources.GetObject(&quot;StickyNote.Icon&quot;))); this.StickyNote.Text = &quot;StickyNote&quot;; this.StickyNote.Visible = true; <span class=cmt>// // sticky // </span> 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(&quot;$this.Icon&quot;))); this.MaximizeBox = false; this.Name = &quot;sticky&quot;; this.ShowInTaskbar = false; this.Text = &quot;Sticky Note, By Saurabh Nandu&quot;; this.TopMost = true; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.ResumeLayout(false); } <span class=cmt>/// &lt;summary&gt; /// This function is called when &quot;Hide&quot; /// is selected from the context menu /// &lt;/summary&gt;</span> protected void minimise (object sender, System.EventArgs e) { if(this.Visible) { <span class=cmt>//Hide the Application</span> this.Hide(); } } <span class=cmt>/// &lt;summary&gt; /// This method is called when &quot;Show&quot; /// is selected from the context Menu /// &lt;/summary&gt;</span> protected void maximise (object sender, System.EventArgs e) { <span class=cmt>//Maximize the Window //Why??? since we are initially setting the Forms //State to &quot;Minimized&quot; in the constructor</span> this.WindowState = System.Windows.Forms.FormWindowState.Normal; if(!this.Visible) { //If the Window is hidden //Show it this.Show(); } } <span class=cmt>/// &lt;summary&gt; /// Method called when &quot;Help&quot; button clicked /// &lt;/summary&gt;</span> protected void helpme (object sender, System.EventArgs e) { MessageBox.Show(&quot;Sticky Note Made by Saurabh Nandu, saurabh@mastercsharp.com&quot;) ; } <span class=cmt>/// &lt;summary&gt; /// Called when &quot;Exit&quot; is selected from the context menu. /// &lt;/summary&gt;</span> protected void Exit (object sender, System.EventArgs e) { this.save(); <span class=cmt>//Call the Dispose Method</span> this.Close(); } <span class=cmt>/// &lt;summary&gt; /// It saves the Data to the Text File /// &lt;/summary&gt;</span> protected void save() { <span class=cmt>//Open a File Stream</span> FileStream fout = new FileStream(&quot;sticky.txt&quot;, FileMode.Truncate, FileAccess.Write, FileShare.Read) ; <span class=cmt>//Get a StreamWriter , since its easy to write string with it.</span> StreamWriter sw = new StreamWriter(fout) ; <span class=cmt>//Write the contents for the RichTextBox</span> sw.Write(tbox.Text); sw.Flush(); <span class=cmt>//Close the Streams</span> sw.Close(); } <span class=cmt>/// &lt;summary&gt; /// This method is called from the constructor /// It read the text from the Text file /// and displays it in the RichTextBox /// &lt;/summary&gt;</span> protected void read() { <span class=cmt>//Open the streams to the file</span> FileStream fin = new FileStream(&quot;sticky.txt&quot;, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ; StreamReader tr = new StreamReader(fin) ; <span class=cmt>//Read the data, I use &quot;ReadToEnd&quot; cause with it you //can read the whole file in one shot !</span> tbox.Text = tr.ReadToEnd(); <span class=cmt>//Close the streams</span> tr.Close(); fin.Close(); } <span class=cmt>/// &lt;summary&gt; /// The main entry point for the application. /// &lt;/summary&gt;</span> public static void Main(string[] args) { Application.Run(new sticky()); } } }</pre> </td> </tr> </table>

Comments

Add Comment