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.
Many thanks
gigemboy
January 29th, 2006, 01:47 PM
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?
OT79
January 29th, 2006, 03:07 PM
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?
HanneSThEGreaT
January 30th, 2006, 03:00 AM
Well to create a Only Numeric Textbox,
you could try using this class provided by MSDN (http://msdn2.microsoft.com/en-us/library/ms229644.aspx)
jmcilhinney
January 30th, 2006, 03:09 AM
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. 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 IfIf you're using VB 2005 there is an Integer.Parse method too.
PramodsNair
January 30th, 2006, 05:16 AM
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
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
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.
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
OT79
January 30th, 2006, 01:29 PM
Thats brill, many thanks for all your help guys!!!!!!!!!!!!!!!!!!!!!!
OT79
January 31st, 2006, 05:24 AM
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
jmcilhinney
January 31st, 2006, 07:07 AM
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.
OT79
January 31st, 2006, 07:59 AM
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!!!!!!!!!!
jmcilhinney
January 31st, 2006, 04:11 PM
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.
PramodsNair
February 1st, 2006, 12:56 AM
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
OT79
February 1st, 2006, 07:30 AM
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?
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.
gigemboy
February 1st, 2006, 11:56 PM
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...
OT79
February 2nd, 2006, 02:31 AM
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
PramodsNair
February 2nd, 2006, 05:53 AM
Dear OT79,
in the frmReport forms code
make the line
For i As Integer = 0 To UBound(sales)
to
For i As Integer = 0 To UBound(sales) - 1
and try once again.
Pramod S Nair
OT79
February 2nd, 2006, 08:31 AM
To PramodsNair,
Just before I say anthing else, many thanks to you for your continued assistance, its very much appreciated!
I ammended the coding to your suggestion and that solved the error. My only problem is that the sales file isn't showing the values calculated? As you will see on the descriptive it needs to show the Buy and Sale price, with the inclusion of the Values? I also need to get them displayed like the following:
Buying Sale
Ref No Type Weight Qty CalcMeth Price Value Price Value
P £ P £
100033 1 20 0 1 14.0 2.80 33.0 6.60
100033 1 20 0 1 13.0 2.70 32.0 6.40
etc..............
Thanks
OT79
February 2nd, 2006, 08:33 AM
p.s. the above example should be displayed with the Buying and Sale above Price and Value, P is under Price and the £ is under Value, it has bunched it all up when I posted it, if unclear!
gigemboy
February 2nd, 2006, 02:52 PM
Well how are they being displayed now?
OT79
February 2nd, 2006, 03:22 PM
I'm not displaying the Sale Value? I also require the Buying (Price & Value) and the Sale (Price & Value) to be displayed in decimal? Is there a way that I can drop the leading zeros also?
Many thanks
PramodsNair
February 3rd, 2006, 10:32 PM
Dear OT79,
Could you please give me detailed info on the way in which you need the report to be shown. i.e, is the price (sales/purchase) are to be parsed in to pound and pence, what is the differnece you mean on the price and value etc. Then i will try to do this report section for you, even if my schedules are a bit tight.
Pramod S Nair
OT79
February 4th, 2006, 06:13 AM
Dear PramodsNair,
Many thanks. On the actual report (i've included a screen shot attachment of how the report is to look) the calculations are as discussed:
CalcMeth 1 Weight * Price
CalcMeth 2 Quantity * Price
Therefore in the first instance (CalcMeth1) the calculations are to be Weight * the Buy Price and the Sale Price, thus giving two seperate values ( Buying Value and Sale Value), this is also the case with the second CalcMeth2, where Quantity is to be Qty * Buy Price and then also Qty * Sale Price. I think currently I'm only performing i.e Weight * Buying price, which is giving only one Value!
* As the sale and buying prices have been entered in tenths (1/10) of a pence on form1, these need to be divided by 1000 to give a value in pounds (£) and pence (p) e.g. 1230 will be £1.23, plus I also need to drop leading zeros?
p.s. I have also included the app in case that is more helpful.
Many thanks for your assistance.
PramodsNair
February 4th, 2006, 08:11 AM
Dear OT79,
i didn't until the last post noticed the need for multiplying both sales and buying with quantity. i got it this time.
i will try to post a prg to meet your need by Monday eve. Weekend :wave: so a bit busy. i am working on it and will post it by Monday. don't worry
Pramod S Nair
OT79
February 4th, 2006, 09:00 AM
Hi PramodsNair:
Many thanks for that, you have been a great help!
Regards
OT
PramodsNair
February 6th, 2006, 01:12 AM
Dear OT79,
I have rebuilt the Report Form's Load event to meet your demands. please use the below given 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) - 1
'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 amt1, amt2
If esale(4) = "1" Then
' the calculation method is 1
'Calculation Method 1 Weight * buy Price
'and weight * sale 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 )
amt1 = esale(2) * esale(5) 'Buy * weight
amt2 = esale(2) * esale(6) 'sale * weight
Else
' the calculation method is 2
'Calculation Method 2 Quantity * Price
'and quantity * sale 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 )
amt1 = esale(3) * esale(5) ' buy * qty
amt2 = esale(3) * esale(6) ' sale * qty
End If
'now rebuild the record with the amt we calculated
TotsalesStr = TotsalesStr & Val(esale(0)) & vbTab & Val(esale(1)) & vbTab & Val(esale(2)) & vbTab & Val(esale(3)) & vbTab & Val(esale(4)) & vbTab & Math.Floor(Val(amt1) / 1000) & vbTab & Val(amt1) Mod 1000 & vbTab & Math.Floor(Val(amt2) / 1000) & vbTab & Val(amt2) Mod 1000 & vbTab & vbNewLine
'TotsalesStr = TotsalesStr & Mid(sales(i), 1, sales(i).length - 1) & vbTab & amt & vbNewLine
Next
RichTextBox1.Text = TotsalesStr
''''''''''''''''
End Sub
I think to display the records properly, you can use a Flex grid instead of writing to a textbox as this can affect the alignment. If you need any help regarding the use of flexgrid pls feel free to contact.
Pramod S Nair
OT79
February 6th, 2006, 10:13 AM
To PramodsNair,
I have implemented the code you have gratefully worked on; however it would appear that it is displaying Vaue (Buy and Sale) as 0? It doesn't appear to be multiplying correctly? Is there a way also of displaying the BuyPrice and Value in decimal when the form2 loads the txt* file?
Many thanks for assistance, sorry to be a pest.
Regards
OT
PramodsNair
February 6th, 2006, 10:21 PM
Dear OT79,
OOPS !!! My Mistake, i just messed the order of Pence and Pound calculation.
I have corrected it and the new line of code will be,
TotsalesStr = TotsalesStr & Val(esale(0)) & vbTab & Val(esale(1)) & vbTab & Val(esale(2)) & vbTab & Val(esale(3)) & vbTab & Val(esale(4)) & vbTab & Val(amt1) Mod 1000 & vbTab & Math.Floor(Val(amt1) / 1000) & vbTab & Val(amt2) Mod 1000 & vbTab & Math.Floor(Val(amt2) / 1000) & vbNewLine
Please reflect the above change and it will sure work (:-) it is working here). Still if you have any probs with the program, feel free to contact.
Sorry for being careless.
Now being for a Pest, it's my pleasure to help others as i myself improved in this industry due to other peoples who were devoted in giving help. So pressing me by asking for assistance is not bothering me at all (hmmm, may be my schedule gets a bit tight ;) , but it's a thrill).
Pramod S Nair
OT79
February 7th, 2006, 04:10 AM
Hi PramodsNair,
That works now; however running the app I have realized that the Buying Price and the Sale Price are the result of the multiplication Weight * Buying Price / Weight * Sale Price? Perhaps I haven’t given you a full enough explanation of the Sales Report display. Under the Buying Price and Sale Price is to be the original user input amounts from the MainMenu (Form1); the resulting Value (Buy & Sale) is the Weight * Buy price, and obviously Weight * Sale Price. Therefore only one calculation needs to be performed for Buy Value and the Sale Value. Is there a way to amending the coding to fit this? Is it also possible to show a decimal point for both Buying – (Price & Value) and Sale – (Buying and Value)?
Many thanks
PramodsNair
February 7th, 2006, 05:42 AM
Dear OT79,
is the original sale & buying price also be parsed to pense and pound ?
Yes, the original Buy Price & Value are to be pence, and yes the output you have displayed above is correct: Buy Price entered by user displayed, Buy Value calculated and displayed by us, same for Sale. Sorry I should have added this much sooner, only realised when I ran the app and saw the outputted results!
Yes that is work now, many thanks to you for all you efforts in helping with this application. One last question, how can I get the user inputted Buy Price and Sale Price to be displayed in decimal also, and only show the Buy Value and Sale Value to two places after the decimal point as it is currently showing three places after the decimal? i.e. £4.50 as opposed to current output 4.500
That works fantastic now, many - many thanks to you for all your efforts!!!
Just one last thing , I now wish to ditch the old VB6 coding I have used on the first form to create and write to a TXT file. I have used the following:
I wish to recreate this with the VB.NET StreamWriter, but so far I have been unsuccessful in my attempts?
OT
gigemboy
February 9th, 2006, 03:51 AM
Below is a short example...
Dim MyWriter as New System.IO.StreamWriter("c:\test.txt",True) ' True is append mode
MyWriter.WriteLine("test") 'writes "test" to the end of the "test.txt" file
MyWriter.Close 'closes the writer....
PramodsNair
February 9th, 2006, 04:02 AM
Dear OT79,
Please add the following line to the very top of your frmMainMenu form's code section
Imports System.IO
Now in the btnPurchase_click code i have made the following change
The below given lines in old code have been removed
Dim s As StreamWriter
s = File.AppendText("TotalSalesFile.txt")
s.WriteLine(TotalSalesFile)
s.Close()
That will get you equipped to use .NET code instead of VB 6 code. It was a pleasure helping you. Post if you have any more doubts
Pramod S Nair
OT79
February 9th, 2006, 04:25 AM
Dear Pramodsnair,
My last and final question! using the coding below, I have the Buy Price and Sale Val showing the correct decimal format, however the Buy Price and Sale Val are showing the incorrect decimal placing, for instance: Weight(20) * Buy Price(220) is showing a Buy Val of 4.400 as opposed to 44! Looking at it, the current coding is working on the first forms input i.e. 220, which when multiplied by Weight (20) would equal 4400! therefore is there a way to ammend this? So Buy Price and SalePrice are in correct format i.e.2.20 and then the Buy Val and Sale Val are taking into account this decimal places, in-order that it can perofrm calculation and output in simliar format i.e. 44.00.
Thanks
OT79
February 9th, 2006, 04:28 AM
oops, meant to say your last coding for TotalsSalesFile, not coding below! The following however does show Buy Price and Sale Price in correct format, however I'm unsure of how this affects everthing!!!
Now change our TotalSalesstr to the below given one
TotsalesStr = TotsalesStr & Val(esale(0)) & vbTab & Val(esale(1)) & vbTab & Val(esale(2)) & vbTab & Val(esale(3)) & vbTab & Val(esale(4)) & vbTab & bp & vbTab & amt1 & vbTab & sp & vbTab & amt2 & vbNewLine
Hope this is what you meant by the last post. I have added the code for filestream in the post given above
Pramod S Nair
OT79
February 9th, 2006, 05:34 AM
Dear Pramodsnair;
WOW, I cannot thank you enough for all your hard work and efforts. Its people like you that make learning VB.NET a pleasure. Many, many thanks again.
p.s. I would like to keep in touch with you if possible? I will not bombard you with questions or ask you to complete apps every two minutes; however having a contact like yourself is invaluable. Would it be possible for you to drop me your email address? If so you could email me at: tnnoliv@aol.com.
OT
PramodsNair
February 9th, 2006, 10:12 AM
Dear OT79,
It's a pleasure. Thanks for your comments.
BTW : i have send a mail to you from my personal id
Pramod S Nair
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.