I am my wits end. I have been toying with this code for class and I am stuck. Every time I run the program it shows what I placed within the list box, however that is it. been on this for nearly two days and I need a fresh set of eyes to guide me into the right direction. Please help.
Imports System.IO 'Allows me to not have to put .IO to specify where my class is located
Public Class Form1
'Victor Campudoni
'Unit 5 Assignment
'7/11/09
Private Sub btnDisplayStocks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayStocks.Click
Dim fmtStr2 As String = "{0, -16} {1, -10}{2, -14}{3, -14}{4, -11}" ' formats output by zones for my arrays and their records.
Dim str() As String = IO.File.ReadAllLines("csvSTOCKS.TXT") 'ReadAllLines taught to me by Kenneth Haugland from MSDN it reads
' every line a long with its records into the array dividing each line with records into a different element.
InitListView() 'sub component which is called.
Dim count As Integer ' declared the variable count.
count = str.GetUpperBound(0) 'gets my upper bound then assigns it to count so I can use for counter.
For i As Integer = 0 To count 'counts from 0 to whatever value count gives then assigns this to i.
' For i As Integer = 0 To str.Count - 1
' another way using the .Count with this way you would not need to declare count, or
' use getupperbound as shown above. Shown to me by Kenneth Haugland from MSDN.
Dim csvData = (str(i).Split(CChar(","))) 'as the For...next loops executes the variable i changes, in this case from 0-4 these
'are placed in str(i) giving us our different elements str(0), str(1), str(2), str(3), str(4). which in the next line are then
'outputted to line1 which is put into the elements of the array.
Dim line1 As String = (String.Format(fmtStr2, csvData(0), csvData(1), csvData(2), csvData(3), csvData(4)))
lstOutput.Items.Add(line1) 'each time this runs it adds a line of with its records to the listbox.
Next
End Sub
Private Sub InitListView()
lstOutput.Items.Clear()
Dim fmtStr1 As String = "{0, -16}{1, -12} {2, -10:D} {3, -12} {4, -11}" 'format for my top line of labels.
Dim fmtStr As String = "{0, -10} {1, 11} {2, 10:D} {3, 12} {4, 11}" 'format for my second line of labels
lstOutput.Items.Add("_______________________________________________________________") 'creates a way to underline or create lines in my listbox.
lstOutput.Items.Add("")
lstOutput.Items.Add(String.Format(fmtStr1, "", "Number", "Date", "Purchase", "Current")) 'my top labels being formatted by zone.
lstOutput.Items.Add(String.Format(fmtStr, "Stock", "of Shares", "Purchased", "Price/Share", "Price/Share")) 'my second line of labels being formatted.
lstOutput.Items.Add("_______________________________________________________________")
lstOutput.Items.Add("")
End Sub
Private Sub btnAddStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStock.Click 'event procedure that enables me to add a stock.
Dim message As String 'declared a variable.
If (TxtStock.Text <> "") And (txtPrice.Text <> "") And (txtNumberShares.Text <> "") And _
(txtDatePurchase.Text <> "") And (TxtPurchasePrice.Text <> "") Then 'if all conditions met it it executes everything in its statement area.
'tihs allows the program to make sure the textboxes have the correct information in them if it does
'not have the correct information the else is executed.
Dim sw As StreamWriter = File.AppendText("csvSTOCKS.TXT") 'uses AppendText to add data.
Dim stock As String = TxtStock.Text 'variables declared and initialized using the textboxes.
Dim price As String = TxtPurchasePrice.Text
Dim numberOfShares As String = txtNumberShares.Text
Dim datePurchased As String = txtDatePurchase.Text
Dim purchasePrice As String = TxtPurchasePrice.Text
Dim lineOfText As String
lineOfText = (stock & "," & numberOfShares & "," & datePurchased & "," & price & _
"," & purchasePrice)
'lineOfText is used to form Comma separated values(CSV) in a string.
sw.WriteLine(lineOfText) 'because of the AppendText used earlier that we initialized "sw" it allows
'us to add information to the file when we use sw.WriteLine.
sw.Close() 'closes are file
TxtStock.Clear() 'clears our texboxes.
TxtPurchasePrice.Clear()
txtNumberShares.Clear()
txtDatePurchase.Clear()
TxtPurchasePrice.Clear()
TxtStock.Focus()
MessageBox.Show("Stock, Price, Number of Shares, Date of Purchase, and Purchase Price have been added to the file.")
'displays a message when done.
Else 'else is executed when the condition is not met and it gives a message.
message = "You must enter all of the information into the required fields."
MessageBox.Show(message, "Information Incomplete") 'uses the MessageBox method to do this.
End If
End Sub
Private Sub btnUpdateStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdateStock.Click
'Update stocks on the file, and has a sub statement call within.
Dim message As String
If TxtStock.Text <> "" Then 'if the textbox has anything other than a space it starts the statements.
If File.Exists("csvSTOCKS.TXT") Then 'checks to see if teh file exist.
ChangeStockInfo() 'sub procedure call statement.
Else 'if the file does not exist it goes to the else and gives messages.
message = "Either no file has yet been created or "
message = message & "the file is not where expected."
MessageBox.Show(message, "File Not Found.")
End If
Else ' with the first IF this is the else that tells you stock textbox is empty.
MessageBox.Show("You must enter a stock.", "Information is imcomplete")
End If
TxtStock.Focus() 'puts the cursor in the stock textbox.
End Sub
Sub ChangeStockInfo() 'sub procedure for changing stocks info.
Dim fmtStr2 As String = "{0, -16} {1, -10}{2, -14}{3, -14}{4, -11}" 'format by zones
Dim str() As String = IO.File.ReadAllLines("csvSTOCKS.TXT") 'ReadAllLines into the str() array.
For i As Integer = 0 To str.Count - 1 'another way to get our upperbound For...Next loop used here to show two different
' ways to do the same thing. .Count will count the number of elements in str() array.
'However since it adds the actual number "5" instead of the upper subscript (4) we must subtract 1 so it
'counts 0-4, instead of 0-5.
Dim csvData = (str(i).Split(CChar(","))) 'see previous comments.
Dim line1 As String = (String.Format(fmtStr2, csvData(0), csvData(1), csvData(2), csvData(3), csvData(4)))
If (TxtStock.Text = csvData(0)) Then 'created a condition if textbox equals the element then start statements.
MySub()
End If
Dim sw As StreamWriter = File.AppendText("TEMP.TXT") 'appends the information back to the file
sw.WriteLine((csvData(0) & "," & csvData(1) & "," & csvData(2) & "," & csvData(3) & _
"," & csvData(4))) 'puts the information back in as one line with all 5 records.
sw.Close()
Next
End Sub
Sub MySub()
Dim stock As String = "" 'variables
Dim price As String = ""
Dim numberOfShares As String = ""
Dim datePurchased As String = ""
Dim purchasePrice As String = ""
Dim lineOfText As String = ""
stock = TxtStock.Text 'initialize variables to the textboxes
numberOfShares = txtNumberShares.Text
datePurchased = TxtPurchasePrice.Text
purchasePrice = TxtPurchasePrice.Text
price = TxtPurchasePrice.Text
Dim sw As StreamWriter = File.AppendText("TEMP.TXT") 'AppendText is used so we can add text, it also creates a file.
lineOfText = stock & "," & numberOfShares & "," & datePurchased & "," & purchasePrice & _
"," & price 'use variables here so I can get my input from the textboxes instead of from the
'elements of the array.
sw.WriteLine(lineOfText) 'writes whats in lineOfText to the text file.
sw.Close()
End Sub
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
TxtStock.Clear() 'clears our texboxes.
TxtPurchasePrice.Clear()
TxtNumberShares.Clear()
TxtDatePurchase.Clear()
TxtPurchasePrice.Clear()
lstOutput.Items.Clear()
End Sub
End Class
Again maybe you can see what I cannot. Thanks.