Calculations using a Rich Text Box

R0gueHunt3R

New member
Joined
Mar 28, 2011
Messages
4
Programming Experience
Beginner
As part of an assignment, I need to add numbers which are displayed in a rich text box. The numbers are read from a text box. The user needs to be able to input the numbers as many times as possible. Since, the numbers all come from the same text box, I am unsure how to retain the old information and add the new information to it.

Since I am not really able to make my point clear, I attach this picture:
RTB.PNG

As you can see, I also have a problem with the heading repeating itself. Is there a way of forcing it to only appear the first time?

Here is the code:

VB.NET:
    Private Sub AddMoreItemsToolStripMenuItem_Click(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  AddMoreItemsToolStripMenuItem.Click

        'Retrieve number from textboxes
        intUnitPrice = Val(UnitPrice_txt.Text)
        intQuantity = Val(Quantity_txt.Text)
        intTotal = Val(Total_txt.Text)
        intReceived = Val(Received_txt.Text)

        'Display the output header
            ItemBox_txt.Text &= ("Quantity") & vbTab &  ("Code") & vbTab & ("Name") & vbTab & ("Price") &  Environment.NewLine

            'Display the Data
            ItemBox_txt.Text &= (intQuantity) & vbTab &  (ItemCode_txt.Text) & vbTab & (ItemName_txt.Text) & vbTab  & (intUnitPrice) & Environment.NewLine

            'Calculations
            intTotal = intUnitPrice * intQuantity
            intBalance = intReceived - intTotal

            'Display result in label
            TotalItems_txt.Text = intQuantity
            Total_txt.Text = intTotal
            Balance_txt.Text = intBalance

    End Sub
 
I would create a class or a structure to hold the info of each item entered individually (I would you a List (Of YourClass) ) and when it comes to displaying them in the RTB you have access to the individual items in the list, just tally them up :)
 
How would I go about doing that? I have checked on using lists in VB, but have turned up nothing useful. And how would I force the heading to only appear the first time?
 
Last edited:
Create a class that holds the data you wish to display (fields, etc.). Create a list object which is a list of the class that you created. Add and Clear this object as needed. When it is time to do a tally just loop through your collection and add up the total. As far as the header you will need some sort of boolean value that states whether the header was already created, and only create the header if that boolean value is false, if true you have a header and should not need to create one, don't forget to set this true when you create a header and set to false when you clear the form or load up your application. Or just write out a header once in the beginning or when you clear and at no other time and you should be good.
 
Am I going about this the right way? Predictably, it generates an "'items' is not a member of 'Systems.Windows.Forms.RichTextBox'" error. How would I correct this?

VB.NET:
   Private Sub AddItem_Menu_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddItem_Menu.Click

        'Retrieve number from textboxes
        intUnitPrice = Val(UnitPrice_txt.Text)
        intQuantity = Val(Quantity_txt.Text)
        intTotal = Val(Total_txt.Text)
        intReceived = Val(Received_txt.Text)

        'Display the Data
        ItemBox_txt.Text &= (intQuantity) & vbTab & (ItemCode_txt.Text) & vbTab & (ItemName_txt.Text) & vbTab & (intUnitPrice) & Environment.NewLine

        'Clear Sales
        ItemCode_txt.Clear()
        ItemName_txt.Clear()
        UnitPrice_txt.Clear()
        Quantity_txt.Clear()

        'Initialization Phase
        intTotal = 0
        QuantityCounter = 0
        PriceCounter = 0

        'Storing
        Do While QuantityCounter < ItemBox_txt.Items.Count
            intQuantity = ItemBox_txt.Items(QuantityCounter)
            intTotalItems += intQuantity
            QuantityCounter += 1
        Loop

        Do While PriceCounter < ItemBox_txt.Items.Count
            intUnitPrice = ItemBox_txt.Items(PriceCounter)
            intTotalItems += intQuantity
            PriceCounter += 1
        Loop


        'Calculations
        intTotal = PriceCounter * QuantityCounter
        intBalance = intReceived - intTotal

        'Display result in label
        TotalItems_txt.Text = QuantityCounter
        Total_txt.Text = intTotal
        Balance_txt.Text = intBalance

    End Sub
 
Back
Top