Pre STory - I've had a lot of problems with this program, and I just
added the last part of it (the add item code) and now an older part of
the program crashes.
Public Structure Stocks
Public stockName As String
Public numShares As Double
Public datePurchased As String
Public purchasePrice As Double
Public currentPrice As Double
End Structure
Public Class frmMain
Inherits System.Windows. Forms.Form
Dim StockData(50) As Stocks
Dim ProfitRow As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.
Friend WithEvents lblStock As System.Windows. Forms.Label
Friend WithEvents txtStock As System.Windows. Forms.TextBox
Friend WithEvents lblCurrentPrice As System.Windows. Forms.Label
Friend WithEvents txtCurrentPrice As System.Windows. Forms.TextBox
Friend WithEvents lblDatePurchase d As System.Windows. Forms.Label
Friend WithEvents lblNumberOfShar es As System.Windows. Forms.Label
Friend WithEvents txtDatePurchase d As System.Windows. Forms.TextBox
Friend WithEvents txtNumberOfShar es As System.Windows. Forms.TextBox
Friend WithEvents btnAddStock As System.Windows. Forms.Button
Friend WithEvents Button2 As System.Windows. Forms.Button
Friend WithEvents btnDisplay As System.Windows. Forms.Button
Friend WithEvents btnShowProfitLo ss As System.Windows. Forms.Button
Friend WithEvents btnQuit As System.Windows. Forms.Button
Friend WithEvents lblPurchasePric e As System.Windows. Forms.Label
Friend WithEvents txtPurchasePric e As System.Windows. Forms.TextBox
Friend WithEvents lstProfitLoss As System.Windows. Forms.ListBox
Friend WithEvents lblProfitLoss As System.Windows. Forms.Label
Friend WithEvents lstOutput As System.Windows. Forms.ListBox
<System.Diagnos tics.DebuggerSt epThrough()Priv ate Sub
InitializeCompo nent()
Me.lblStock = New System.Windows. Forms.Label
Me.txtStock = New System.Windows. Forms.TextBox
Me.lblCurrentPr ice = New System.Windows. Forms.Label
Me.txtCurrentPr ice = New System.Windows. Forms.TextBox
Me.lblDatePurch ased = New System.Windows. Forms.Label
Me.lblNumberOfS hares = New System.Windows. Forms.Label
Me.txtDatePurch ased = New System.Windows. Forms.TextBox
Me.txtNumberOfS hares = New System.Windows. Forms.TextBox
Me.btnAddStock = New System.Windows. Forms.Button
Me.Button2 = New System.Windows. Forms.Button
Me.btnDisplay = New System.Windows. Forms.Button
Me.btnShowProfi tLoss = New System.Windows. Forms.Button
Me.btnQuit = New System.Windows. Forms.Button
Me.lblPurchaseP rice = New System.Windows. Forms.Label
Me.txtPurchaseP rice = New System.Windows. Forms.TextBox
Me.lstOutput = New System.Windows. Forms.ListBox
Me.lstProfitLos s = New System.Windows. Forms.ListBox
Me.lblProfitLos s = New System.Windows. Forms.Label
Me.SuspendLayou t()
'
'lblStock
'
Me.lblStock.Loc ation = New System.Drawing. Point(16, 8)
Me.lblStock.Nam e = "lblStock"
Me.lblStock.Siz e = New System.Drawing. Size(40, 23)
Me.lblStock.Tab Index = 0
Me.lblStock.Tex t = "Stock"
'
'txtStock
'
Me.txtStock.Loc ation = New System.Drawing. Point(64, 8)
Me.txtStock.Nam e = "txtStock"
Me.txtStock.Siz e = New System.Drawing. Size(100, 20)
Me.txtStock.Tab Index = 1
'
'lblCurrentPric e
'
Me.lblCurrentPr ice.Location = New System.Drawing. Point(184, 8)
Me.lblCurrentPr ice.Name = "lblCurrentPric e"
Me.lblCurrentPr ice.Size = New System.Drawing. Size(72, 23)
Me.lblCurrentPr ice.TabIndex = 2
Me.lblCurrentPr ice.Text = "Current Price"
'
'txtCurrentPric e
'
Me.txtCurrentPr ice.Location = New System.Drawing. Point(264, 8)
Me.txtCurrentPr ice.Name = "txtCurrentPric e"
Me.txtCurrentPr ice.Size = New System.Drawing. Size(72, 20)
Me.txtCurrentPr ice.TabIndex = 3
'
'lblDatePurchas ed
'
Me.lblDatePurch ased.Location = New System.Drawing. Point(400, 8)
Me.lblDatePurch ased.Name = "lblDatePurchas ed"
Me.lblDatePurch ased.Size = New System.Drawing. Size(100, 32)
Me.lblDatePurch ased.TabIndex = 4
Me.lblDatePurch ased.Text = "Date Purchased (mm/dd/yy)"
'
'lblNumberOfSha res
'
Me.lblNumberOfS hares.Location = New System.Drawing. Point(544,
8)
Me.lblNumberOfS hares.Name = "lblNumberOfSha res"
Me.lblNumberOfS hares.Size = New System.Drawing. Size(88, 32)
Me.lblNumberOfS hares.TabIndex = 5
Me.lblNumberOfS hares.Text = "Number of Shares"
'
'txtDatePurchas ed
'
Me.txtDatePurch ased.Location = New System.Drawing. Point(400,
48)
Me.txtDatePurch ased.Name = "txtDatePurchas ed"
Me.txtDatePurch ased.Size = New System.Drawing. Size(100, 20)
Me.txtDatePurch ased.TabIndex = 6
'
'txtNumberOfSha res
'
Me.txtNumberOfS hares.Location = New System.Drawing. Point(544,
48)
Me.txtNumberOfS hares.Name = "txtNumberOfSha res"
Me.txtNumberOfS hares.Size = New System.Drawing. Size(104, 20)
Me.txtNumberOfS hares.TabIndex = 7
'
'btnAddStock
'
Me.btnAddStock. Location = New System.Drawing. Point(72, 48)
Me.btnAddStock. Name = "btnAddStoc k"
Me.btnAddStock. Size = New System.Drawing. Size(75, 23)
Me.btnAddStock. TabIndex = 8
Me.btnAddStock. Text = "Add Stock"
'
'Button2
'
Me.Button2.Loca tion = New System.Drawing. Point(160, 48)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing. Size(104, 23)
Me.Button2.TabI ndex = 9
Me.Button2.Text = "Update Stock"
'
'btnDisplay
'
Me.btnDisplay.L ocation = New System.Drawing. Point(72, 80)
Me.btnDisplay.N ame = "btnDisplay "
Me.btnDisplay.S ize = New System.Drawing. Size(75, 23)
Me.btnDisplay.T abIndex = 10
Me.btnDisplay.T ext = "Display"
'
'btnShowProfitL oss
'
Me.btnShowProfi tLoss.Location = New System.Drawing. Point(160,
80)
Me.btnShowProfi tLoss.Name = "btnShowProfitL oss"
Me.btnShowProfi tLoss.Size = New System.Drawing. Size(104, 23)
Me.btnShowProfi tLoss.TabIndex = 11
Me.btnShowProfi tLoss.Text = "Show Profit/Loss"
'
'btnQuit
'
Me.btnQuit.Loca tion = New System.Drawing. Point(280, 64)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.Size = New System.Drawing. Size(75, 23)
Me.btnQuit.TabI ndex = 12
Me.btnQuit.Text = "Quit"
'
'lblPurchasePri ce
'
Me.lblPurchaseP rice.Location = New System.Drawing. Point(400,
88)
Me.lblPurchaseP rice.Name = "lblPurchasePri ce"
Me.lblPurchaseP rice.Size = New System.Drawing. Size(100, 23)
Me.lblPurchaseP rice.TabIndex = 13
Me.lblPurchaseP rice.Text = "Purchase Price"
'
'txtPurchasePri ce
'
Me.txtPurchaseP rice.Location = New System.Drawing. Point(512,
88)
Me.txtPurchaseP rice.Name = "txtPurchasePri ce"
Me.txtPurchaseP rice.Size = New System.Drawing. Size(136, 20)
Me.txtPurchaseP rice.TabIndex = 14
'
'lstOutput
'
Me.lstOutput.Lo cation = New System.Drawing. Point(24, 112)
Me.lstOutput.Na me = "lstOutput"
Me.lstOutput.Si ze = New System.Drawing. Size(624, 134)
Me.lstOutput.Ta bIndex = 15
'
'lstProfitLoss
'
Me.lstProfitLos s.FormattingEna bled = True
Me.lstProfitLos s.Location = New System.Drawing. Point(24, 275)
Me.lstProfitLos s.Name = "lstProfitL oss"
Me.lstProfitLos s.Size = New System.Drawing. Size(620, 186)
Me.lstProfitLos s.TabIndex = 16
Me.lstProfitLos s.Visible = False
'
'lblProfitLoss
'
Me.lblProfitLos s.AutoSize = True
Me.lblProfitLos s.Font = New System.Drawing. Font("Verdana",
14.25!, System.Drawing. FontStyle.Bold,
System.Drawing. GraphicsUnit.Po int, CType(0, Byte))
Me.lblProfitLos s.Location = New System.Drawing. Point(20, 249)
Me.lblProfitLos s.Name = "lblProfitL oss"
Me.lblProfitLos s.Size = New System.Drawing. Size(196, 23)
Me.lblProfitLos s.TabIndex = 17
Me.lblProfitLos s.Text = "Profit/Loss Table" &
Global.Microsof t.VisualBasic.C hrW(13) &
Global.Microsof t.VisualBasic.C hrW(10)
Me.lblProfitLos s.Visible = False
'
'frmMain
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(656, 473)
Me.Controls.Add (Me.lblProfitLo ss)
Me.Controls.Add (Me.lstProfitLo ss)
Me.Controls.Add (Me.lstOutput)
Me.Controls.Add (Me.txtPurchase Price)
Me.Controls.Add (Me.lblPurchase Price)
Me.Controls.Add (Me.btnQuit)
Me.Controls.Add (Me.btnShowProf itLoss)
Me.Controls.Add (Me.btnDisplay)
Me.Controls.Add (Me.Button2)
Me.Controls.Add (Me.btnAddStock )
Me.Controls.Add (Me.txtNumberOf Shares)
Me.Controls.Add (Me.txtDatePurc hased)
Me.Controls.Add (Me.lblNumberOf Shares)
Me.Controls.Add (Me.lblDatePurc hased)
Me.Controls.Add (Me.txtCurrentP rice)
Me.Controls.Add (Me.lblCurrentP rice)
Me.Controls.Add (Me.txtStock)
Me.Controls.Add (Me.lblStock)
Me.Name = "frmMain"
Me.Text = "Stock Analysis"
Me.ResumeLayout (False)
Me.PerformLayou t()
End Sub
#End Region
Private Sub btnQuit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnQuit.Click
End
End Sub
Private Sub btnDisplay_Clic k(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnDisplay.Clic k
Dim indexPicked As Integer
indexPicked = lstOutput.Selec tedIndex
Select Case indexPicked '' maybe there is a better way to do
this? maybe not
Case indexPicked
txtStock.Text = StockData((inde xPicked)).stock Name
txtPurchasePric e.Text =
StockData((inde xPicked)).purch asePrice
txtDatePurchase d.Text =
StockData((inde xPicked)).dateP urchased
txtCurrentPrice .Text =
StockData((inde xPicked)).curre ntPrice
txtPurchasePric e.Text =
StockData((inde xPicked)).purch asePrice
txtNumberOfShar es.Text =
StockData((inde xPicked)).numSh ares
End Select
End Sub
Private Sub frmMain_Load(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
IntializeData() ' Loading data from CSV routine
End Sub
Sub IntializeData()
Dim arr() As String
Dim row, i, displayStop As Integer
Dim sLine As String
Dim fstream As IO.StreamReader = New IO.StreamReader (New
IO.FileStream(" csvstocks.txt", IO.FileMode.Ope n))
Dim fmtStr As String = "{0,-35}{1,-15}{2,25}{3,15} {4,40}"
row = 0
Do While fstream.Peek <-1 '' Load everything up!
sLine = fstream.ReadLin e
arr = sLine.Split("," c)
StockData(row). stockName = arr(0)
StockData(row). numShares = arr(1)
StockData(row). datePurchased = arr(2)
StockData(row). purchasePrice = arr(3)
StockData(row). currentPrice = arr(4)
row = row + 1
Loop
ProfitRow = row '' asigns the max value to use later in the
Profit/Loss routine
fstream.Close()
i = 0
displayStop = row
Do Until i = displayStop '' Tricky nice way of doing that huh?
lstOutput.Items .Add(String.For mat(fmtStr,
StockData((i)). stockName, StockData((i)). numShares,
StockData((i)). datePurchased, StockData((i)). purchasePrice,
StockData((i)). currentPrice))
row = row - 1
i = i + 1
Loop
End Sub
Private Sub btnShowProfitLo ss_Click(ByVal sender As System.Object,
ByVal e As System.EventArg s) Handles btnShowProfitLo ss.Click
lstProfitLoss.V isible = True
lblProfitLoss.V isible = True
lstProfitLoss.I tems.Clear()
Dim fmtStrProfit As String = "{0,55}{1,55}{2 ,45} {3,25}"
Dim tmpArray(50, 4) As String '' 50 rows should be enough for
this program
Dim j, xi As Integer
xi = ProfitRow
j = 0
' First write the 0 places (stock name)
Do Until j = ProfitRow
tmpArray(j, 0) = StockData((j)). stockName
j = j + 1
Loop
j = 0 ' reset this back to zero for each of the loops to do its
magic
Dim x As Integer ' need another variable for the next stages
x = 0
' Now lets do some massive math
Do Until j = ProfitRow
tmpArray(j, 1) = StockData((j)). numShares *
StockData((j)). purchasePrice
j = j + 1
Loop
j = 0 ' zero back out
Do Until j = ProfitRow
tmpArray(j, 2) = StockData((j)). numShares *
StockData((j)). currentPrice
j = j + 1
Loop
j = 0 ' zero out again!
Do Until j = ProfitRow
tmpArray(j, 3) = tmpArray(j, 2) - tmpArray(j, 1)
j = j + 1
Loop
j = 0
Do Until j = ProfitRow ' finally list them
lstProfitLoss.I tems.Add(String .Format(fmtStrP rofit,
tmpArray(j, 0), tmpArray(j, 1), tmpArray(j, 2), tmpArray(j, 3)))
j = j + 1
Loop
End Sub
Private Sub btnAddStock_Cli ck(ByVal sender As System.Object, ByVal
e As System.EventArg s) Handles btnAddStock.Cli ck
Dim sw As IO.StreamWriter = IO.File.AppendT ext("csvstocks. txt")
Dim tmpName, tmpDate As String
Dim tmpNum, tmpPurchase, tmpCurrent As Double
tmpName = txtStock.Text
tmpNum = txtNumberOfShar es.Text
tmpDate = txtDatePurchase d.Text
tmpPurchase = txtPurchasePric e.Text
tmpCurrent = txtCurrentPrice .Text
Dim tmpString() As String = {"tmpName", "tmpNum", "tmpDate",
"tmpPurchas e", "tmpCurrent "}
sw.WriteLine(St ring.Join(","c, tmpString))
sw.Close()
End Sub
End Class
This is the error code that always shows up
Error 1 'frmMain' is a type in 'StockAnalysis' and cannot be used as an
expression. F:\Programming
Projects\StockA nalysis\Form1.v b 0 0 StockAnalysis
That's all the code, it crashes when you run it on
StockData(row). numShares = arr(1) saying its out of bounds.
This was working just a little bit before I put the Add stock button
code, even when I take out the code the error is still there and the
program crashes. I've probably deleted some part of code on accident
and can't figure it out. Thanks for the help