Simple Newbie Question

integra
02-05-2004, 06:37 PM
I know this is probably really straightforward to most of you, but I just can't work it out!!

I'm basically trying to write a program which adds up a customer's bill at a restaurant.

Here's the bit of code i'm having trouble with...

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
TextBox4.Text = TextBox5.Text * TextBox6.Text
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Select Case ComboBox2.SelectedIndex
Case 0
TextBox6.Text = 0.0
Case 1
TextBox6.Text = 3.5
Case 2
TextBox6.Text = 4.0
Case 3
TextBox6.Text = 3.0
End Select
End Sub

Basically i'm trying to multiply the values in textboxes 5 and 6 as you can see. But it won't let me do this since the text in box6 is a String.

Here's a rundown of what I have so far. The customer chooses their meal from a drop down menu, by doing this a number is inserted into nother text box (see above numbers). The customer then enters how many servings they want into another textbox (box5). Then the program is supposed to multiply the 2 numbers in boxes 5 and 6 and enter them into box 4. I just can't figure this out! SO I hope that's enough info for you.

Thanks in advance for any help.

BasiclyVisual
02-05-2004, 06:57 PM
you should put vb tags around code, but i dont see why it won't work. you can times a string and a string, provided that they contain a number:

Dim a As String
Dim b As String
a = "3"
b = "4"
Dim c As String
c = a * b
MsgBox c

but which line is it erroring on?

integra
02-05-2004, 07:02 PM
It errors at the line :

TextBox4.Text = TextBox5.Text * TextBox6.Text

Basically tells me that two of the boxes are strings or something like that.

I've attached the whole coding as a text file if that helps.

Elven Commander
02-05-2004, 07:10 PM
this may make a difference
when i've preformed that operation, i've always used a variable. I've never even tried it that way. txtbox1.text * txtbox2.text etc. set them to integers, or whatever suits your needs. and you should be able to output these into a textbox, your integer value, or if you need, put it into a string. But i've always used variables for this, just my thought.
EC

integra
02-05-2004, 07:15 PM
Sorry, but that all just went completely over my head. I've only been doing this a couple of days so I really only know the very basics.

Care to elaborate? :D Thanks!

Elven Commander
02-05-2004, 07:27 PM
ok,

lets say you have txtA.Text and txtB.Text as input

Dim intA as integer 'declare variables
Dim intB as integer
Dim intSum as integer

intA = txtA.Text
intB = txtB.Text

intSum = intA + intB
lblOutput.Caption = lblOutput.Caption & intSum

There are other ways i'm sure, this is just what i do. you set your variables a and b to your intput. and then i just use a emptry variable to dump to, and in the intsum line, you just do what ever operation you need.

integra
02-05-2004, 07:39 PM
Ok, thanks. I did that, but it still came up with an error. It said..."Cast from String "" to type 'Integer' is not valid".

I changed the code to this...

Dim intA As Integer
Dim intB As Integer
intA = TextBox5.Text
intB = TextBox6.Text
TextBox4.Text = intA * intB

Instead of....

TextBox4.Text = TextBox5.Text * TextBox6.Text

The line where the error occured was.."intB = TextBox6.Text"

Any other ideas?

Elven Commander
02-05-2004, 07:54 PM
ok, i'm not sure about the problem, but here is my input.


you are trying to take a menu and multply the costs or whatever and display the answer. justkeep in mind i'm not expert. So you have a combo box, that's cool. Just have it if a value is selcted than an integer is given a numerical value that corresponds.

i can't remeber off the top of my head how to put data into them, its been a long while since i've programmed, and i don't have vb on here, but this is what youd be doing.

if combo1 = "fries" then
intCost = 1
End if
if combo1 = "burger" then
intCost = 2
end if
etc etc etc

also, just myself coding i wouldn't put this project in a sub, but that's just me. I would put a calculate button or something on the frm, and when it is clicked, it goes through the if statements, and then

intA = txtblah.Text etc
and remeber one of the integers is used in the if statements so you don't have to do anything to that one.

the Dim statements should be in the frmload, or above the code under option explicit.
i would do the multiplication and display the output all under the caculate button. and along with the calculate button, i also like to have a clear button etc


I'm not sure if this is what your going for, and i may also not make sense cause i'm quite tired, but give it a whirl
hopefully you can get an idea of where i'm comming from and i make some sense.

EC

jumentous
02-05-2004, 08:00 PM
i am assuming you rproblem is because you are assigning to the wrong data types
in Private Sub TextBox5_KeyPress(KeyAscii as Integer) & in TextBox6 put

If not ((KeyAscii >= vbkey0 and Keyascii <= vbKey9) or KeyAscii = vbKeyBack) Then
KeyAscii = 0
End if

That will only allow the keys 0 - 9 and the backspace key to be entered into the box, so you may need to accept like a decimal place as well Note that integers CAN NOT accept numbers with a decimal place

One You have done that you should be able to say
IntA = cInt(TextBox5.text)
IntB = cInt(TextBox6.text)

integra
02-05-2004, 08:53 PM
Still can't get this thing to work! This time I've attached a small screenshot of what I've got so far to give you all a better idea of what i'm trying to do.

So basically. The user selects their food from the drop down menu and the price appears in the box under "price". The user then enters a number into the box under "no.". The program then multiplies the "price" and "number" values and enters the total in the box under "total".

So far the program will shove the price of the food in the price column, but that's pretty much it.

If I change the line "TextBox4.Text = TextBox5.Text * TextBox6.Text" to "TextBox4.Text = TextBox5.Text + TextBox6.Text" it puts the "price" followed by the "no." in the end box. So if the food was 3.50 and they wanted 2 servings, it would appear as 3.502. So it's basically treating the text in box6 as a string, all I need to fine out is how to save it as an integer or double so I can multiply the numbers together.

Thanks to everyone for their help so far! Sorry to be a pain! :)

kjlith
02-06-2004, 12:55 AM
You should convert the textbox strings into values.


a = Val(Textbox4.Text)
b = Val(Textbox5.Text)
c = a * b
Text6.Text = c

This works on my program.

Elven Commander
02-06-2004, 07:51 AM
post your code, as you have it at your last update attempt. I'd like to take a look at it after i'm done my cisco reading, providing someone doesn't beat me to it.

integra
02-06-2004, 08:15 AM
That's it! It now does what I wanted so I thank you!

Now, next problem! :D

When it posts the end total in the last box it just posts it as a single number with no decimals. I'd like it to be in the form "0.00". So instead of it saying 3 or 3.5 it will say 3.00 or 3.50.

Thanks again to all who helped!

P.S:. I've attached the new code.

Iceplug
02-06-2004, 08:23 AM
Use: TB4.Text = c.ToString("0.00")
(you should be using ToString for all conversions from number to text)
:).
(Also, Double.Parse instead of Val)

integra
02-06-2004, 08:28 AM
Hey, thanks for the quick reply! Man this forum is good.

The "Double.Parse" rather than "Val" didn't work, it gave me errors. But I changed the other bit and it works exactly as I wanted it now.

Thanks again. No doubt I'll be back later on with more questions! :p

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum