Solved

need help figuring out the coding

Posted on 2011-03-17
22
271 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:goldfingerpunk
  • 11
  • 7
  • 4
22 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35159824
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?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35159871
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...
0
 

Author Comment

by:goldfingerpunk
ID: 35160315
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
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35160436
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?
0
 

Author Comment

by:goldfingerpunk
ID: 35160538
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35160573
"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?
0
 

Author Comment

by:goldfingerpunk
ID: 35160583
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35160600
When you added those items to the ListBoxes...did you simply add the .Text() property value directly from the TextBoxes?
0
 

Author Comment

by:goldfingerpunk
ID: 35160768
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
0
 

Author Comment

by:goldfingerpunk
ID: 35160787
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()
0
 

Author Comment

by:goldfingerpunk
ID: 35160875
and this is frustrating as hell because i cant get the total into the total text box
0
 

Author Comment

by:goldfingerpunk
ID: 35161086
i need help anyone
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35161132
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

0
 

Author Comment

by:goldfingerpunk
ID: 35161184
the adding the items to the listbox is working fine it adds as many as i want to i did test that out
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35161207
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

0
 

Author Comment

by:goldfingerpunk
ID: 35161283
ok that worked but what does the i stand for
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35161353
>>  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?
0
 

Author Comment

by:goldfingerpunk
ID: 35161389
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.
0
 

Author Comment

by:goldfingerpunk
ID: 35161397
would it be (i) would be the text
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35161576
>>  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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35161640
>>  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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35161873
This is basically the same thing as I posted previously back here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26894525.html#35161132

...but nested together to handle all three inputs from the user:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim quantity As Integer
        Dim price As Decimal

        If Integer.TryParse(QuantityTextBox.Text, quantity) AndAlso quantity > 0 Then
            If MenuItemTextBox.Text.Trim <> "" Then
                If Decimal.TryParse(PriceTextBox.Text, price) AndAlso price > 0 Then
                    QuantityListBox.Items.Add(quantity)
                    MenuItemListBox.Items.Add(MenuItemTextBox.Text)
                    PriceListBox.Items.Add(price)

                    QuantityTextBox.Clear()
                    MenuItemTextBox.Clear()
                    PriceTextBox.Clear()

                    CalculateTotal()

                    QuantityTextBox.Focus()
                Else
                    MessageBox.Show("Please enter a valid price greater than zero.")
                    PriceTextBox.Focus()
                    PriceTextBox.SelectAll()
                End If
            Else
                MessageBox.Show("Please enter a menu item.")
                MenuItemTextBox.Focus()
                MenuItemTextBox.SelectAll()
            End If
        Else
            MessageBox.Show("Please enter a valid quantity greater than zero.")
            QuantityTextBox.Focus()
            QuantityTextBox.SelectAll()
        End If
    End Sub

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with consolidating excel files using VB.net 2 29
Help with converting xml file to excel using VB.NET 3 25
Logon script fails 23 45
randomize data from excell cell 15 46
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question