Re: Help with application
In order to read and write files, you should look into streamreaders and streamwriters, that is the ".NET" way :).
I am wondering why you have a numeric up/down in order to choose the calculations. Wouldn't two radio buttons be more appropriate?
Re: Help with application
Hi:
Yes I know, however I have to include this as this is the way they want it! In terms of the second form performing the calculations would I simply do this after I have called the TXT file?
Re: Help with application
Re: Help with application
You should be using a TryParse method to attempt to parse the TextBox contents without throwing an exception if it fails. The way you are doing it your app will crash if the user enters a non-numerical value. Also, pretend that the NumericUpDown has no Text property and use its Value property instead. Finally, use ToString to format strings rather than Format. E.g.
Code:
Dim numVal As Double
If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Integer, Nothing, numVal) Then
Me.TextBox1.Text = numVal.ToString("d6")
Else
MessageBox.Show("Please enter an integral value.")
Exit Sub
End If
If you're using VB 2005 there is an Integer.Parse method too.
Re: Help with application
Dear ,
I think the following code changes will help you in getting you in to the path of solving theproject in hand. Even though the way you are using is not the optimal one i am giving you a quick and dirty way to make the code work for you in the way you want. :-)
Change the code in btnPurchase_click in frmMainMenu to the below given one
Code:
Private Sub btnPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
txtRef.Text = Format(CInt(txtRef.Text), "000000")
nudProductType.Text = Format(CStr(nudProductType.Text), "0")
txtWeight.Text = Format(CInt(txtWeight.Text), "0000")
txtQuantity.Text = Format(CInt(txtQuantity.Text), "0000")
''''I commented out the below line
' nudCalcMeth.Text = Format(CStr(nudCalcMeth.Text), "0")
''''''
txtBuy.Text = Format(CInt(txtBuy.Text), "0000")
txtSale.Text = Format(CInt(txtSale.Text), "0000")
Dim TotalSalesFile As String
'put fields together to go to Sales Text File
'''''''''
'i added vbtabs after each field
TotalSalesFile = txtRef.Text & vbTab & nudProductType.Text _
& vbTab & txtWeight.Text & vbTab & txtQuantity.Text & vbTab & nudCalcMeth.Value _
& vbTab & txtBuy.Text & vbTab & txtSale.Text
FileOpen(1, "TotalSalesFile.txt", OpenMode.Append)
PrintLine(1, TotalSalesFile)
FileClose(1)
If MessageBox.Show("Click Yes or No", "Reset", MessageBoxButtons.YesNo) = DialogResult.Yes Then
If MessageBox.Show("Do you wish to clear this purchase?", "Reset", MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
'Clears existing purchase
txtRef.Text = ""
nudProductType.Text = ""
txtWeight.Text = ""
txtQuantity.Text = ""
nudCalcMeth.Text = ""
txtBuy.Text = ""
txtSale.Text = ""
Else
End If
End If
End Sub
Now in the frmReport change form load to the code below.
Code:
Private Sub frmReport2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This subrouting uses a StreamReader object to open an existing file when the form loads
Dim ReportStream As StreamReader
ReportStream = _
New StreamReader("TotalSalesFile.txt")
'''Made a Change Here
Dim tempstr As String
'RichTextBox1.Text = ReportStream.ReadToEnd
tempstr = ReportStream.ReadToEnd
''''''''''
ReportStream.Close()
'RichTextBox1.Select(0, 0)
''''''''''''''
'Another change
' The quick and dirty way of handling
'Added by Pramod S Nair
Dim TotsalesStr As String
Dim sales
sales = Split(tempstr, vbLf) ' Split your file using Linefeed character to get the individual sales entries
For i As Integer = 0 To UBound(sales)
'Now split each sales record using Tab as
'we added a vbtab after each entry while
'constructing the sales record.
Dim esale
esale = Split(sales(i), vbTab)
'Now we have each field in esale
' check the fifth array item to find way of calculation
Dim amt
If esale(4) = "1" Then
' the calculation method is 1
'Calculation Method 1 Weight * Price
'The 3rd member of esale array is weight and now multiply
' it with your price (Here i presume the 6th one that is buying
' is the amount is the price you are talking about )
amt = esale(2) * esale(5)
Else
' the calculation method is 2
'Calculation Method 2 Quantity * Price
'The 4th member of esale array is qty and now multiply
' it with your price (Here i presume the 6th one that is buying
' is the amount is the price you are talking about )
amt = esale(3) * esale(5)
End If
'now rebuild the record with the amt we calculated
TotsalesStr = TotsalesStr & Mid(sales(i), 1, sales(i).length - 1) & vbTab & amt & vbNewLine
Next
RichTextBox1.Text = TotsalesStr
''''''''''''''''
End Sub
This will get you the calculated results at the end of each record according to the calculation you want.
Pls delete any old records that you have in the TotalSalesFile.txt before using the code.
Pramod S Nair
Re: Help with application
Thats brill, many thanks for all your help guys!!!!!!!!!!!!!!!!!!!!!!
1 Attachment(s)
Re: Help with application
Hi all:
I implemented the code PramosdNair suggested and I have met an unhandled error at the following point:
If esale(4) = "1" Then
' the calculation method is 1
'Calculation Method 1 Weight * Price
'The 3rd member of esale array is weight and now multiply
' it with your price (Here i presume the 6th one that is buying
' is the amount is the price you are talking about )
amt = esale(2) * esale(5)
Else
' the calculation method is 2
'Calculation Method 2 Quantity * Price
'The 4th member of esale array is qty and now multiply
' it with your price (Here i presume the 6th one that is buying
' is the amount is the price you are talking about )
amt = esale(3) * esale(5)
End If
'now rebuild the record with the amt we calculated
TotsalesStr = TotsalesStr & Mid(sales(i), 1, sales(i).length - 1) & vbTab & amt & vbNewLine
Next
RichTextBox1.Text = TotsalesStr
''''''''''''''''
End Sub
Could anyone help me further? I have included an application summary as my descriptive was somewhat vauge!
Thanks in advance
Re: Help with application
You haven't told us what the error message was or what line it occurred on. I think it's fair to say that that is fairly important information.
Re: Help with application
Sorry for that! Error message is on the line of:
If esale(4) = "1" Then
(An unhandled exception of type 'System.Index.OutOfRange.Exception' occurred in mscorlib.dll
Additional information: Index was outside the bounds of the array)
Also when the calculation is performed in frmReport I require it to create an amount under the headings of Value (showing decimal) thiis I have also been unable to do?
Any help much appreciated!!!!!!!!!!
Re: Help with application
You are using an index of 4 and the exception is telling you that that index is out of range. That means that your array does not have that many elements.
Re: Help with application
Dear OT79 ,
Pls delete any old records that you have in the TotalSalesFile.txt before using the code. Completely Wipe out the old records created by the old code. Then try this.
If it still makes and error on the statement you noted (If esale(4) = "1" Then
) please report.
Pramod S Nair
Re: Help with application
Hi:
Yes, I cleared the sales file and the error message was still presented? In terms of this application I don’t know if you have been able to view the descriptive; however I have noticed that the data doesn’t need to be placed into an array, as it doesn’t need to be sorted. Therefore If I were to firstly split the 24 string into substrings is the following correct?
‘Split data into individual fields
txtRef = LineOfText.Substring(0, 6)
nudProductType = LineOfText.Substring(6, 1)
txtWeight = LineOfText.Substring(7, 4)
txtQty = LineOfText.Substring(11, 4)
nudCalcMeth = LineOfText.Substring(15, 1)
txtBuy = LineOfText.Substring(16, 4)
txtSale = LineOfText.Substring(20, 4
How would I then put the above into individual variables? Then format them to the correct method i.e.
txtBuy = Format(CDec(Stringfromsalesfile.substring(16, 4) / 10),
If I was to then perform necessary calculations using an If Then statement, i.e:
If nudCalcMeth = 1 Then
BuyValue = Format(((Weight * BuyPrice) / 100), “0.00”)
Then display the data in the RichTextBox? Current the 24 string doesn’t include the Valuep; therefore if I were to use the above CalcMeth how do I get it into the TotalSalesFile, which is to be displayed in the RT?
Any help on this matter is very much appreciated.
Re: Help with application
Your tempstr variable does not contain the correct information, or this information is different. You split tempstr, but in the split, it does not create an array with 5 indexes or more... thus you are getting the error when you try to access the 5th element in the array (at "If esale(4) = "1""), as it does not exist (as JM suggested). Find out what your tempstr variable contains, and you will probably figure out what the error is... simple debugging techniques...
Re: Help with application
Hi:
The TotalSalesFile is a 24 string file therefore do I declare the tempsr as this or is it:
For i As Integer = 24 To UBound (sales)?
Thanks