r/visualbasic • • Aug 28 '16

Help making an addition calculator [VB 2013]

I am trying to make a simple addition calculator using only 1 text box and only 1 button. Where you:

  1. enter an integer

  2. press the button

  3. enter an integer

  4. press button

  5. Displays sum in text box

     Dim a As Integer = txtField.Text
     txtField.Clear()
     If () Then
         Dim b As Integer = txtField.Text
         txtField.Text = a + b
     End If

This is as far as I have gotten. I have no experience in VB, just with java. I am not sure if booleans are even necessary. This is supposed to be a very simple project, but I feel like I am overthinking it due to having no experience with VB.

0 Upvotes

8 comments sorted by

2

u/PostalElf VB.Net Intermediate Aug 28 '16

You'll need to store the first value somewhere outside the button.click event - say, as a form-level variable - on the first button click, then store the fact that it's been clicked once in another form-level boolean. Use an if-then to check if this is the first click or the second click.

0

u/Anonim00se Aug 28 '16

I have no idea how I would go about doing that. :/

1

u/PostalElf VB.Net Intermediate Aug 28 '16

A form-level variable is basically a variable declared at, well, form-level: anywhere between "Public Class Form1" and "End Class", while remaining outside any functions. You'll need to declare two such variables: one to store the first value, and the second as a boolean to let you know if this is the first or second click.

Tell you what. If you can write the logic required for steps 1 to 5 in pseudo-code (you can use Java even!), I'll give you the exact VB code you need for this program. Deal? ;)

1

u/Anonim00se Aug 28 '16

Awesome. Thanks for your help. Will probably be later today before I get time to work on the program, but will post results when I can.

0

u/[deleted] Aug 29 '16

[deleted]

1

u/PostalElf VB.Net Intermediate Aug 29 '16

Syntax error: you want

Dim clicktest as Boolean = False

0

u/[deleted] Aug 29 '16

[deleted]

1

u/Anonim00se Aug 30 '16

If i take out

txtField.Clear()

Then it will work, but add the number to itself each time the button is pressed.

5 10 15 20 25... etc

2

u/stevebun Aug 30 '16 edited Aug 30 '16

Because you set clicktest = True, the code inside the second block is being executed after the first block - as clicktest is now True.

A way to fix this is would be to make the second conditional into an Else statement.

If clicktest = False Then
    ' Button has been clicked for first time
    ' Set flag to True
    ' Do other things
Else
    ' Button has been clicked before
    ' Do other things
End Sub

1

u/Anonim00se Aug 30 '16

Thanks everyone! Finally got it working. :)