I have two forms; frmMain & frmReport. I have the following coding for ensuring that the numeric data entered into each of the TextBoxes in in the correct format:
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")
nudCalcMeth.Text = Format(CStr(nudCalcMeth.Text), "0")
txtBuy.Text = Format(CInt(txtBuy.Text), "0000")
txtSale.Text = Format(CInt(txtSale.Text), "0000")
My first question is that this is VB6 and I would like to know a better way of coding this event process?
Following on from this I then have all the data saved to a string which is then saved to a *txt file:
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")
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
TotalSalesFile = txtRef.Text & nudProductType.Text _
& txtWeight.Text & txtQuantity.Text & nudCalcMeth.Text _
& txtBuy.Text & txtSale.Text
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
My second form, frmReport is to read the TotalSalesFile.txt and display the data. My problem here is that I have a NumericUpDown (nudCalcMeth), which has two options (1 or 2) Therefore this is calculated as follows:
My second form has to perfom these calculation when it load and the using Tab print the data. I totaly unsure of how to do this and would very much appreciate someones assistance.
I have included the app in case someone wants to view it and give feedback.
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?
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.
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.
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!
(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?
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.
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.
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?
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?
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...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.