Getting NASDAQ Quotes with a Pocket PC in VB.NET

If you have been lucky enough to get the Compact Framework or Smart devices extension beta for April 2002 you may be wondering what you can do with it. After previous articles showing some simple C# code I though why not do something with the Internet and getting stock quotes is a good idea.

However before delving into this article ask yourself why, bearing in mind that Pocket PC comes with a pretty good web browser would you write an application to access a web site directly? The simple reason is that most web sites do not consider that a small device like a Pocket PC may access them and so write web pages assuming everyone is running on a large screen. Of course a pocket PC screen is typically one quarter the size of a VGA screen so you can see the problem.

Even so if you accessed nasdaq.com using the Pocket PC browser it would do a fairly good job of trying to fit all the information on screen. Of course all this takes time and as web sites get more complicated the browser on the Pocket PC does a lot of work working out how to present information designed for a large screen on to the small Pocket PC screen.

That is why being able to write programs like this and bypass a web browser altogether can be useful.

Here is what the application runs like on my Pocket PC emulator.

GettingQuotesImgage-in-vb.net.jpg

As you can see from the code I am using the WebRequest and WebResponse classes to talk to
www.nasdaq.com. I read the response into a string and then search through it using the IndexOf method of the string class. Using this simple technique allows me to search through the returned HTML code and extract the data I need. Of course if Nasdaq.com change their pages then it’s likely this code will break.

Once again I tested this code on the Pocket PC emulator and it worked fine if slightly slowly. Some things to note where this program could be improved.

  1. Program will fail if an invalid symbol is entered.
  2. I create a WebRequest for each symbol rather than grouping them all into one request. This was purely done to make the code simpler to read.
    Lastly, to show that this program is waiting for web responses I display an hourglass using Pocket PC APIs to inform the user the program is actually busy.

When I get more time later versions of this program will gather more Nasdaq information and allow you to do things like symbol lookups etc.

'PocketPCNasdaq.cs
'Lets you check stock quotes over internet on a Pocket PC
'Written using Compact Framework Beta 1
'Written June 4th 2004 by John O'Donnell - [email protected]
Imports System
Imports System.Windows.Forms
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private hourGlassCursorID As Integer = 32514
'Pocket PC API's to show and hide waitcursor
Private Declare Function LoadCursor Lib "coredll.dll" (ByVal zeroValue As Integer, ByVal cursorID AsInteger) As Integer
Private
 Declare Function SetCursor Lib "coredll.dll" (ByVal cursorHandle As Integer) As Integer
Private
 label1 As System.Windows.Forms.Label
Private label6 As System.Windows.Forms.Label
Private label5 As System.Windows.Forms.Label
Private label4 As System.Windows.Forms.Label
Private label3 As System.Windows.Forms.Label
Private label2 As System.Windows.Forms.Label
Private textBox1 As System.Windows.Forms.TextBox
Private textBox2 As System.Windows.Forms.TextBox
Private textBox3 As System.Windows.Forms.TextBox
Private textBox4 As System.Windows.Forms.TextBox
Private textBox5 As System.Windows.Forms.TextBox
Private button1 As System.Windows.Forms.Button
Private label7 As System.Windows.Forms.Label
Private label8 As System.Windows.Forms.Label
Private panel1 As System.Windows.Forms.Panel
#
Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)MyBase.Dispose(disposing)
End Sub
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer. 
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.label1 = New System.Windows.Forms.Label
Me.label6 = New System.Windows.Forms.Label
Me.label5 = New System.Windows.Forms.Label
Me.label4 = New System.Windows.Forms.Label
Me.label3 = New System.Windows.Forms.Label
Me.label2 = New System.Windows.Forms.Label
Me.textBox1 = New System.Windows.Forms.TextBox
Me.textBox2 = New System.Windows.Forms.TextBox
Me.textBox3 = New System.Windows.Forms.TextBox
Me.textBox4 = New System.Windows.Forms.TextBox
Me.textBox5 = New System.Windows.Forms.TextBox
Me.button1 = New System.Windows.Forms.Button
Me.panel1 = New System.Windows.Forms.Panel
Me.label8 = New System.Windows.Forms.Label
Me.label7 = New System.Windows.Forms.Label
'
'label1
'
Me.label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Bold)
Me.label1.Location = New System.Drawing.Point(16, 8)
Me.label1.Size = New System.Drawing.Size(160, 32)
Me.label1.Text = "Nasdaq Quotes"
'
'label6
'
Me.label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold)
Me.label6.Location = New System.Drawing.Point(112, 152)
Me.label6.Size = New System.Drawing.Size(64, 24)
'
'label5
'
Me.label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold)
Me.label5.Location = New System.Drawing.Point(112, 120)
Me.label5.Size = New System.Drawing.Size(64, 24)
'
'label4
'
Me.label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold)
Me.label4.Location = New System.Drawing.Point(112, 88)
Me.label4.Size = New System.Drawing.Size(64, 24)
'
'label3
'
Me.label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold)
Me.label3.Location = New System.Drawing.Point(112, 56)
Me.label3.Size = New System.Drawing.Size(64, 24)
'
'label2
'
Me.label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold)
Me.label2.Location = New System.Drawing.Point(112, 24)
Me.label2.Size = New System.Drawing.Size(64, 24)
'
'textBox1
'
Me.textBox1.Location = New System.Drawing.Point(16, 24)
Me.textBox1.Size = New System.Drawing.Size(64, 22)
Me.textBox1.Text = ""
'
'textBox2
'
Me.textBox2.Location = New System.Drawing.Point(16, 56)
Me.textBox2.Size = New System.Drawing.Size(64, 22)
Me.textBox2.Text = ""
'
'textBox3
'
Me.textBox3.Location = New System.Drawing.Point(16, 88)
Me.textBox3.Size = New System.Drawing.Size(64, 22)
Me.textBox3.Text = ""
'
'textBox4
'
Me.textBox4.Location = New System.Drawing.Point(16, 120)
Me.textBox4.Size = New System.Drawing.Size(64, 22)
Me.textBox4.Text = ""
'
'textBox5
'
Me.textBox5.Location = New System.Drawing.Point(16, 152)
Me.textBox5.Size = New System.Drawing.Size(64, 22)
Me.textBox5.Text = ""
'
'button1
'
Me.button1.Location = New System.Drawing.Point(80, 184)
Me.button1.Size = New System.Drawing.Size(112, 24)
Me.button1.Text = "Retrieve Quotes"
AddHandler button1.Click, AddressOf button1_Click
'
'panel1
'
Me.panel1.BackColor = System.Drawing.Color.Transparent
Me.panel1.Controls.Add(Me.label8)
Me.panel1.Controls.Add(Me.label7)
Me.panel1.Controls.Add(Me.label6)
Me.panel1.Controls.Add(Me.label5)
Me.panel1.Controls.Add(Me.label4)
Me.panel1.Controls.Add(Me.label3)
Me.panel1.Controls.Add(Me.label2)
Me.panel1.Controls.Add(Me.textBox1)
Me.panel1.Controls.Add(Me.textBox2)
Me.panel1.Controls.Add(Me.textBox3)
Me.panel1.Controls.Add(Me.textBox4)
Me.panel1.Controls.Add(Me.textBox5)
Me.panel1.Controls.Add(Me.button1)
Me.panel1.Location = New System.Drawing.Point(16, 32)
Me.panel1.Size = New System.Drawing.Size(200, 216)
'
'label8
'
Me.label8.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold)
Me.label8.Location = New System.Drawing.Point(104, 8)
Me.label8.Size = New System.Drawing.Size(56, 16)
Me.label8.Text = "Last Sale"
'
'label7
'
Me.label7.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold)
Me.label7.Location = New System.Drawing.Point(16, 8)
Me.label7.Size = New System.Drawing.Size(64, 16)
Me.label7.Text = "Symbol"
'
'Form1
'
Me.Controls.Add(Me.panel1)
Me.Controls.Add(Me.label1)
Me.Text = "PocketPCNasdaq"
End Sub
Shared
 Sub Main()
Application.Run(
New Form1)
End Sub 'Main
#End Region
Private
 Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.ShowWaitCursor(True)
If textBox1.Text <> "" Then
label2.Text = GetQuote(textBox1.Text)
End If
If
 textBox2.Text <> "" Then
label3.Text = GetQuote(textBox2.Text)
End If
If
 textBox3.Text <> "" Then
label4.Text = GetQuote(textBox3.Text)
End If
If
 textBox4.Text <> "" Then
label5.Text = GetQuote(textBox4.Text)
End If
If
 textBox5.Text <> "" Then
label6.Text = GetQuote(textBox5.Text)
End If
Me
.ShowWaitCursor(False)
End Sub
'Returns the current value for a particular stock
Public Function GetQuote(ByVal symbol As String) As String
Dim
 a As String
Dim
 b As String
Dim
 c As String
Dim
 d As String = ""
Dim query As String = http://quotes.nasdaq.com/Quote.dll?mode=stock&symbol=
query = query + symbol + "&&quick.x=0&quick.y=0"
Dim wrq As WebRequest = WebRequest.Create(query)
' Return the response. 
Dim wr As WebResponse = wrq.GetResponse()
Dim sr As New StreamReader(wr.GetResponseStream(), Encoding.ASCII)
a = sr.ReadToEnd().ToString()
If a.IndexOf("data") > -1 Then
Dim
 pos1 As Integer = a.IndexOf("<")
Dim pos2 As Integer = a.IndexOf(">")
b = a.Substring(pos1 + 1, pos2 - pos1)
pos1 = b.IndexOf(";")
c = b.Substring(pos1, 10)
pos1 = c.IndexOf(",")
d = c.Substring(1, pos1 - 2)
End If
'close resources between calls
wr.Close()
sr.Close()
Return d
End Function 'GetQuote
' Shows or Hides the wait cursor on the PPC
Public Sub ShowWaitCursor(ByVal ShowCursor As Boolean)
Dim cursorHandle As Integer = 0
If ShowCursor Then
cursorHandle = LoadCursor(0, hourGlassCursorID)
End If
SetCursor(cursorHandle)
End Sub 'ShowWaitCursor
End Class


Similar Articles