CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    Join Date
    Jan 2006
    Posts
    20

    Red face Help with application

    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

    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

    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:

    Calculation Method 1 Weight * Price
    Calculation Method 2 Quantity * Price

    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
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Posts
    293

    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?

  3. #3
    Join Date
    Jan 2006
    Posts
    20

    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?

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Help with application

    Well to create a Only Numeric Textbox,
    you could try using this class provided by MSDN

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    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.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  6. #6
    Join Date
    Apr 2005
    Location
    India
    Posts
    271

    Smile 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
    Learn by Sharing.

    You can use some free things i have done from www.wisdombay.com

  7. #7
    Join Date
    Jan 2006
    Posts
    20

    Talking Re: Help with application

    Thats brill, many thanks for all your help guys!!!!!!!!!!!!!!!!!!!!!!

  8. #8
    Join Date
    Jan 2006
    Posts
    20

    Question 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
    Attached Files Attached Files

  9. #9
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    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.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  10. #10
    Join Date
    Jan 2006
    Posts
    20

    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!!!!!!!!!!

  11. #11
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    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.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  12. #12
    Join Date
    Apr 2005
    Location
    India
    Posts
    271

    Smile 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
    Learn by Sharing.

    You can use some free things i have done from www.wisdombay.com

  13. #13
    Join Date
    Jan 2006
    Posts
    20

    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.

  14. #14
    Join Date
    Jan 2006
    Posts
    293

    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...

  15. #15
    Join Date
    Jan 2006
    Posts
    20

    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

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured