Link to home
Start Free TrialLog in
Avatar of goldfingerpunk
goldfingerpunk

asked on

need help figuring out the coding

Hi
I need to figure out how to code for a assignment and I'm confused I'm making a restaruant bill and I have to figure out when I click the add item button how to get the quantity, menu item and price over to the 3 matching listbox and what they should be dimed as also then I have to calculate the total of the items in the three lists box and get the total to a total text box. I wpuld really appriciate any help.
Avatar of kaufmed
kaufmed
Flag of United States of America image

We are not allowed to provide you with specific answers (nor should you be seeking such) due to academic integrity. We can, however, point out where you may be going astray or give you an idea why an approach may/may not work.

Can you post any code you've started?
In addition to posting any code...please tell us what types of controls you've chosen to use and how they are setup in your GUI.  A labeled screenshot would be helpful...
Avatar of goldfingerpunk
goldfingerpunk

ASKER

ok well what do i dim menu items as cause its not an integer and am i suppose to dim the total as double cause that is what i did so far
That really depends on HOW you are going to present the menu to the user.

Are you going to give them CheckBoxes, RadioButtons, a ListBox?...or are they going to enter a menu item number into a TextBox?

How many items are we talking here?...are there any specifics in the assignment that dictate what controls must be used?
no i basically building it from scratch there are the items: quantity (which is how much of each item they want) i was guessing that was an integer then there is menu item and this one im not sure on because its text not a number so i dont know what to dim that as and then there is the price which i assumed is dimed as decimal and total which i wanna say is dimed as double. And i just figured out how to get the quantity, menu item and price to there individual list box so im moving on to the add total button. This is where i get confused on how to dim what and what goes in the loop. Oh btw thanks for not just giving me the code it helped figuring out the first part with the add button now come the tough part for me at least though
"And i just figured out how to get the quantity, menu item and price to there individual list box so im moving on to the add total button."

Ok...so it sounds like you three individual ListBoxes: quantity, item, price
(and corresponding rows from the ListBoxes constitute a "record")

I assume you have STRINGS in those ListBoxes right now?
this is how i dimed everything so far and i dont think its all right most of it i think is but here is what i got
Dim quantity As Integer = 0
        Dim item As Object = (MenuItemTextBox.Text)
        Dim price As Decimal = 0
        Dim total As Double = 0
When you added those items to the ListBoxes...did you simply add the .Text() property value directly from the TextBoxes?
I get this error message with the line:quantity = QuantityListBox.Items.Item(QuantityListBox.Text)
   Conversion from string "" to type 'Integer' is not valid.

Here is what i have coded so far

 Dim quantity As Integer = 0
        Dim item As Object = (MenuItemTextBox.Text)
        Dim price As Decimal = 0
        Dim total As Double = 0


        Do
            quantity = QuantityListBox.Items.Item(QuantityListBox.Text)
            price = PriceListBox.Items.Item(PriceListBox.Text)
            total = price * quantity
        Loop Until price And quantity = PriceListBox.Items.Count And QuantityListBox.Items.Count


        total = price * quantity
        TotalLabel.Text = String.Format("{0:F}", total)
        QuantityTextBox.Focus()



    End Sub 'totalbutton_click
End Class
yeah what i did when i added the items to the list boxes was

'clear previous information
        If TotalLabel.Text <> "" Then
            TotalLabel.Text = ""
            QuantityListBox.Items.Clear()
            MenuItemListBox.Items.Clear()
            PriceListBox.Items.Clear()
        End If

        'display information in list box
        QuantityListBox.Items.Add(Val(QuantityTextBox.Text))
        MenuItemListBox.Items.Add(MenuItemTextBox.Text)
        PriceListBox.Items.Add(PriceTextBox.Text)
        QuantityTextBox.Clear()
        MenuItemTextBox.Clear()
        PriceTextBox.Clear()
        QuantityTextBox.Focus()
and this is frustrating as hell because i cant get the total into the total text box
i need help anyone
You are clearing out the ListBoxes every time?  Why have ListBoxes then if you're only going to have one thing?...

Let's start with adding new items.  The first thing you should do is validate the textboxes to ensure valid data has been entered.  Obviously the menu item is just a string so for that you'd just need to make sure it is not blank.  For the quantity, an integer greater than zero would be required.  Finally, for the price, a valid decimal value greater than zero would be good.

If all three values pass then you simply add them to their respective boxes.

For example, here is how to make sure that an Integer greater than zero has been entered into TextBox1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim value As Integer
        If Integer.TryParse(TextBox1.Text, value) AndAlso value > 0 Then

            ' ... do something with "value" in here ...

        Else
            MessageBox.Show("Please enter an integer greater than zero.")
        End If
    End Sub

Open in new window

the adding the items to the listbox is working fine it adds as many as i want to i did test that out
Ok...assuming you have valid quantity and price values in your ListBoxes, you could compute the total like this:
Private Sub CalculateTotal()
        Dim Total As Decimal = 0
        Dim quantity As Integer
        Dim price As Decimal
        Dim subTotal As Decimal

        For i As Integer = 0 To QuantityListBox.Items.Count - 1
            quantity = CInt(QuantityListBox.Items(i))
            price = CDec(PriceListBox.Items(i))
            subTotal = quantity * price
            Total = Total + subTotal
        Next
        TotalLabel.Text = "Total: " & Total.ToString("C")
    End Sub

Open in new window

ok that worked but what does the i stand for
>>  ok that worked but what does the i stand for

Think of it in this manner and tell us what you think "i" stands for:

Saying, "For i As Integer" is similar to saying, "Dim i As Integer."

What is "i" in the latter statement?
there is just one more part my teacher added and its the error check part. Its if one or more text boxes are empty display error message (when the add item button is clicked) and do not display anything in the list boxes so far what i have is
If QuantityTextBox.Text = "" Or MenuItemTextBox.Text = "" Or PriceTextBox.Text = "" Then
            MsgBox("Information is missing in one or more text boxes!")
            QuantityListBox.Items.Add(Val(QuantityTextBox.Text)) = False

but the quantitylistbox line is underlined in blue so im doing something wrong ive never had to make something not make something happen in this program yet.
would it be (i) would be the text
>>  would it be (i) would be the text

Time for a lesson in syntax   = )

.NET has the concept of collections, which you can think of as super-charged arrays. Collections can indexed (individual slots accessed) much the same way arrays can. VB uses parentheses as the indexing operator. You may have confused this for a function call--one of VB's shortcomings for new developers. In this case, QuantityListBox.Items is a collection and in the code Idle_Mind posted it is being indexed. "i" is just the variable being used to hold the value of the index you want to inspect at that point in time when the code is being executed. If you erased "i" and put "1", then you would be accessing the 2nd slot of the QuantityListBox.Items collection. Since you are in a loop, "i" is being used to access successive slots of QuantityListBox.Items.

What Idle_Mind's code is doing at that line is converting the value at item index "i" in QuantityListBox.Items and converting that string into an integer.
>>  but the quantitylistbox line is underlined in blue so im doing something wrong

Take a look at what you're doing:

QuantityListBox.Items.Add(Val(QuantityTextBox.Text)) = False

Open in new window


What this code *appears* to do is add the string found in QuantityTextBox to the QuantityListBox, BUT, it is also trying to assign the value of "False" to the object returned by Add(). However, Add() returns an INTEGER so trying to assign False doesn't make sense.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial