| |||
Views: 88,501,973 |
Main | FAQ | Uploader | IRC chat | Radio | Memberlist | Active users | Latest posts | Calendar | Stats | Online users | Search | 04-29-24 06:27 PM |
|
Guest: Register | Login |
0 users currently in Computing | 1 guest |
Main - Computing - Visual Basic help, really basic. | New thread | New reply |
darkwitch |
| ||
Paragoomba Level: 20 Posts: 52/65 EXP: 41519 Next: 920 Since: 02-21-07 From: Puerto Rico Last post: 6224 days Last view: 6224 days |
So, I am trying to learn a third programing language and I choosed Visual Basic (6.0), I find it quite easy and decided to code a calculator, I managed to make the value add or subtract when I hit the equals button, etc... Here is the problem:
If instep = 0 Then If method = subtract Then res.Text = val1 - val2 End If If method = Add Then res.Text = val1 + val2 End If End If val1 = 0 val2 = 0 method = "" End Sub That's triggered when I hit the equals button, well, lets say I tried "1+1" it brought 2, but if I put next "5-3" it gives me 8. I am sure the problem is with the "method" variable but I can't figure it out. The Add button changes the "method" variable to "Add". The subtract button changes the "method" variable to "subtract". Also, I haven't figured how to add a number to an integer like: I have 'number' as "5" if I hit the 6 button it replaces it to "6" instead of changing it to '56'. I tried using += but that just gives me an error. Help would be really appreciated |
Sukasa |
| ||
Red Birdo Level: 92 Posts: 119/2112 EXP: 7691395 Next: 65542 Since: 02-19-07 Last post: 4451 days Last view: 3223 days |
Try
If instep = 0 Then If method = "Subtract" Then 'If it's subtracting... res.Text = val1 - val2 Else 'otherwise add res.Text = val1 + val2 End If End If val1 = 0 val2 = 0 method = "" End Sub |
HyperHacker |
| ||
... Level: 73 Posts: 222/1220 EXP: 3367679 Next: 118189 Since: 03-25-07 From: no Last post: 6094 days Last view: 6078 days |
Posted by darkwitchYou have to do it the long way in VB: x = x + foo. |
darkwitch |
| ||
Paragoomba Level: 20 Posts: 53/65 EXP: 41519 Next: 920 Since: 02-21-07 From: Puerto Rico Last post: 6224 days Last view: 6224 days |
In the '1' button I have:
val1 = val1 + 1 ^ But that wouldn't increment val1 by one? Like if val1 = 4 it would change it to 5 instead of 41. |
Sukasa |
| ||
Red Birdo Level: 92 Posts: 120/2112 EXP: 7691395 Next: 65542 Since: 02-19-07 Last post: 4451 days Last view: 3223 days |
Can you post your full code here? |
darkwitch |
| ||
Paragoomba Level: 20 Posts: 54/65 EXP: 41519 Next: 920 Since: 02-21-07 From: Puerto Rico Last post: 6224 days Last view: 6224 days |
Declarations:
Dim method As String Dim val1 As Integer Dim val2 As Integer Dim instep As Integer Private Sub equals_Click() If instep = 0 Then MsgBox ("You must add a second value to add or subtract!") Else If method = "subtract" Then res.Text = val1 - val2 End If If method = "Add" Then res.Text = val1 + val2 End If End If val1 = 0 val2 = 0 method = "" End Sub Private Sub minus_Click() instep = 1 res.Text = "" method = "subtract" End Sub Private Sub num0_Click() If instep = 0 Then val1 = val1 + 0 res.Text = val1 Else val2 = val2 + 0 res.Text = val2 End If End Sub ' I dont think I need to post all number buttons since they are the same as num0 Private Sub plus_Click() instep = 1 method = "Add" res.Text = "" End Sub There it has the equals button, the plus and the minus, and the 0 button. Here is another problem, if I hit 4 - 3 before hitting the equals, I get on the text box "7" and if hit equals I get "-7". |
Sukasa |
| ||
Red Birdo Level: 92 Posts: 121/2112 EXP: 7691395 Next: 65542 Since: 02-19-07 Last post: 4451 days Last view: 3223 days |
Try adding "instep = 0" to the equals_click() event. |
darkwitch |
| ||
Paragoomba Level: 20 Posts: 55/65 EXP: 41519 Next: 920 Since: 02-21-07 From: Puerto Rico Last post: 6224 days Last view: 6224 days |
Cool that fixed it, now the only problem is that when I hit the "5" button and then again, instead of putting "55" it puts "10". Is the "val1 = val1 + x" thingy. But I can't figure a way to make the 'x' go after val1 instead of increasing it by "x".
|
Sukasa |
| ||
Red Birdo Level: 92 Posts: 122/2112 EXP: 7691395 Next: 65542 Since: 02-19-07 Last post: 4451 days Last view: 3223 days |
Hm, try this: Dim Val1/2 as a string, and add "5" not 5, the same for the other numbers. Then, in equals_click, make is res.text = str(val(val1) =/- val(val2)) |
darkwitch |
| ||
Paragoomba Level: 20 Posts: 56/65 EXP: 41519 Next: 920 Since: 02-21-07 From: Puerto Rico Last post: 6224 days Last view: 6224 days |
Finally it's fixed, yeah that worked. Thanks a bunch!
|
Sukasa |
| ||
Red Birdo Level: 92 Posts: 123/2112 EXP: 7691395 Next: 65542 Since: 02-19-07 Last post: 4451 days Last view: 3223 days |
Welcome
The reason I said to use strings is that val1+5 originally added five to a number, whereas using strings adds "5" to the end of the string, and you can just convert the string to a number So, instead of 5+5 = 10, you're going "5" + "5" = "55" |
Main - Computing - Visual Basic help, really basic. | New thread | New reply |
© 2005-2023 Acmlm, blackhole89, Xkeeper et al. |
MySQL - queries: 82, rows: 101/102, time: 0.016 seconds. |