Stock Tracker

This program is a utility to track stock quotes for multiple symbols.


This program is a utility to track stock quotes for multiple symbols. The program runs in the system tray and displays stock quotes for one or more symbols.

The main form runs in the Windows system tray. When the user clicks on the program icon in the system tray, the utility fetches the stock quotes for the specified symbols and displays them in a message box. The user can also modify the selections for the stock symbols. To modify the selections, right click in the system tray and select the option Edit Symbols. Enter the list of symbols in comma-separated format. To exit the stock tracker utility, right click on the program icon in the system tray and select the option Exit Stock Tracker.

The .Net Framework Systems.Windows.Forms contains the classes for windows forms. The NotifyIcon control provides the functionality for running the program as an icon in the system tray. The ContextMenu control is used to provide the system menu for the icon in the system tray.

Programming Using Visual Studio.Net (Beta2).

New Project:

Create a new project named ST in Visual Studio.Net of type Visual C# Project, Template - Windows Application.

Initialize Form1:

Change the following properties for Form1.cs in design view, using the Property Editor.

WindowsState = Minimized
ShownInTaskBar = False

Control to run in the system tray:

Add a NotifyIcon control from the Windows controls in the toolbox.

Change the property Icon to point to the .ico file to be displayed in the system tray. I have included a sample icon file with the sample files.

Context menu for the system tray icon:

Add a ContextMenu control from the toolbox and change the following properties:

Right Click on the Context Menu and select the option Edit Menu.

In the Form Design, Click on Context Menu and replace the words in the first submenu Type Here with Edit Stock Symbols and in the next sub-menu below, type in Exit Stock Tracker as shown in Figure 1.



Figure 1: Menu Editor

Click on the NotifyIcon Control and change the following property in the Property Editor:

ContextMenu: contextMenu1

Starting adding the code: Double click on the NotifyIcon. This will take you to the code view and also add the Dispose function code for the Notify Icon.

Import Namespaces: Add the following code in the beginning of the code

using System.Net;
using System.Text;
using System.IO;

Declarations: Add the following code in the class declarations section

private static string strerror;
private string strQuoteSymbol="MSFT";
private string[] strQuoteArray;
private char[] separator = {','} ;
private static string strcurindex;
private static string strcurdate;

Initialize: Add the following code in the constructor of the form 

notifyIcon1.Click += new EventHandler(this.notifyIcon1_Click);
//read the file and initialize the string.
StreamReader sr = new StreamReader("symbols.txt");
string strLine = sr.ReadLine();
strQuoteArray = strLine.Split(separator) ;
sr.Close();
this.Hide();

Code for displaying the stock quotes from the System Tray Icon: Add the following code to the class

private void notifyIcon1_Click(object sender, System.EventArgs e)
{
string strQuotes;
int i;
strQuotes = "";
for(i=0;i<strQuoteArray.Length;i++)
{
string strResult = GetQuoteFromWebSite(strQuoteArray[i]);
string[] temp = strResult.Split(separator) ;
if(temp.Length >1)
{
//We only show the relevant portions .
strcurindex = "Current Index :"+temp[1] ;
strcurdate ="Last Update on"+temp[2]+" at "+temp[3] ; strQuotes = strQuotes + strcurindex + System.Environment.NewLine + "Last updated: " + strcurdate + System.Environment.NewLine;
}
else
{
strerror ="Error :"+ strResult ;
strQuotes = strQuotes + strerror + System.Environment.NewLine;
}
}
MessageBox.Show(strQuotes, "Stock Quotes");
}

Core Functionality: Add the following code

private static String GetQuoteFromWebSite(String strsymbol)
{
string fullpath =@"http://quote.yahoo.com/d/quotes.csv?s="+ strsymbol +"&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";
HttpWebRequest req;
HttpWebResponse res;
StreamReader sr;
string strResult;
try
{
req = (HttpWebRequest) WebRequest.Create(fullpath);
res = (HttpWebResponse) req.GetResponse();
sr =
new StreamReader(res.GetResponseStream(), Encoding.ASCII);
strResult = sr.ReadLine();
sr.Close();
}
catch(Exception)
{
strResult = "Exception occured";
}
return strResult;
}

Code for context menus functionality: Add the following code

private void menuItem2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
Form2 frm2 =
new Form2(this);
frm2.Show();
frm2.Activate();
}

Code for
updating symbols list : Add the following code to Form1.cs

public void UpdateSymbols(string strNewSymbols)
{
strQuoteSymbol = strNewSymbols;
StreamWriter sw =
new StreamWriter("symbols.txt", false, Encoding.ASCII);
sw.WriteLine("{0}", strQuoteSymbol);
sw.Close();
strQuoteArray = strNewSymbols.Split(separator) ;
}
public string CurrentSymbols()
{
return strQuoteSymbol;
}

Form for allowing the user to modify the stock symbols:

Right click on the Project Name (ST) and Add a New Windows Form Form2.cs

UI for editing the symbols:

Add a label control, change the Text property to Enter Stock Symbols. Add a textbox control and change the Name property to txtSymbol. Add a button control, change its text property to Update and Name property to btnUpdate.
See Figure 2.



Figure 2: UI for Updating the Symbols 

Update Symbols Implementation:

Double click the button control and add the following code in the btnUpdate_Click function:

form1.UpdateSymbols(txtSymbol.Text);
this.Close();

Add the following code to the constructor of Form2.

btnUpdate.Click += new EventHandler(this.btnUpdate_Click);

Declarations for Form2:

Add the following code to the delcarations section of Form2.

public Form1 form1;

Initialization for Form2:

Add the new constructor shown below in the code :

public Form2(Form1 frm1)
{
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
form1 = frm1;
}

Add the following code to the class :

private void Form2_Load(object sender, System.EventArgs e)
{
txtSymbol.Text = form1.CurrentSymbols();
}

Build the Application:

Click Ctrl_Shift+B to Build the application.

Initialization for running the Application:

Important: Create a text file, type in MSFT and save as symbols.txt in the same directory as the application exe file.

Here is the Stock Tracker in action:



Figure 3: Stock Traker in Action

To edit the symbols, right click the Stock Tracker icon and select Edit Stock Symbols. Enter the list of symbols in a comma separated format eg. : MSFT,NT.

To exit the Stock Tracker, click on Exit Stock Tracker.

To check the quotes, click on the Stock Tracker icon.

Issues :

The windows form containing the NotifyIcon control does not get hidden using the Form.Hide() function. For now, I have used a workaround to virtually hide the form, by setting ShowInTaskBar as false and WindowState as Minimize.

References: