VB.net help

  dave_the_red 17:34 03 Dec 05
Locked

I have recently installed VB.net and am trying to learn Vb programming in my spare time, I have only been doing it one day so am still learning the basics.

First of all is there anybody who could help me if i post a sample of the code i have written to solve a little problem that is frustrating me so much?

Basically i have been following an example to design a calculator program but in the example it only has a + button, so i decided to give a minus button a go next.

My problem is that the minus button works but when the answer appears on the display of the calculator it has a minus sign before the answer eg if i was to calculate 5 - 2 the answer displayed would be -3

I will post the code if there is anybody that knows vb.net.

Thanks

Dave

  Access genu 17:41 03 Dec 05

you will have to send in the sign to the calculate routine

  dave_the_red 17:45 03 Dec 05

Sorry but i am total newbie to this, like i said i have only really been learning this for a couple of hours today, so i am a bit lost with your reply.

This is the code that i use for my minus button

Private Sub cmdMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
total1 = total1 - Val(txtDisplay.Text)
txtDisplay.Clear()

  Access genu 18:07 03 Dec 05

what is the value of total1 before you start?

  dave_the_red 18:09 03 Dec 05

Maybe better to post the full code then, see if you can make head or tail of it. Thanks for helping Access :-)


Public Class Form1
Inherits System.Windows.Forms.Form

Dim total1 As Integer
Dim total2 As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
txtDisplay.Text = txtDisplay.Text & btn0.Text
End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
txtDisplay.Text = txtDisplay.Text & btn1.Text
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
txtDisplay.Text = txtDisplay.Text & btn2.Text
End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
txtDisplay.Text = txtDisplay.Text & btn3.Text
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
txtDisplay.Text = txtDisplay.Text & btn4.Text
End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
txtDisplay.Text = txtDisplay.Text & btn5.Text
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
txtDisplay.Text = txtDisplay.Text & btn6.Text
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
txtDisplay.Text = txtDisplay.Text & btn7.Text
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
txtDisplay.Text = txtDisplay.Text & btn8.Text
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
txtDisplay.Text = txtDisplay.Text & btn9.Text
End Sub

Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click
total1 = total1 + Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub

Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
total2 = total1 + Val(txtDisplay.Text)
txtDisplay.Text = total2
total1 = 0

End Sub

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
txtDisplay.Clear()
End Sub

Private Sub cmdMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
total1 = total1 - Val(txtDisplay.Text)
txtDisplay.Clear()

End Sub
End Class

  Access genu 18:40 03 Dec 05

in the dim statement add the following

dim blnMinus as boolean

Change the btnPlus to the following

total1 = Val(txtDisplay.Text)
txtDisplay.Clear()
blnMinus = false

Change the btnMinus to the following

total1 = Val(txtDisplay.Text)
txtDisplay.Clear()
blnMinus = true

change btnEquals to

select case blnMinus
case true 'Do the Plus
total2 = total2 + (total1 + Val(txtDisplay.Text))
case False 'Do the minus
total2 = total2 + (total1 - Val(txtDisplay.Text))
end select
txtDisplay.Text = total2
total1 = 0

  Skills 21:32 03 Dec 05

Dont know if you'll find this of any help but I found them on MSDN the other day. Its a series of 16 videos for the new vb.net 05 express editions which you can download for free. They look like there quite good i've only watched the first couple so far click here

  Access genu 22:40 03 Dec 05

Ok try the following code, you should be able to replace your existing code directly

Public Enum WhatToDo
my_ADD = 0
my_SUBTRACT = 1
my_MULTIPY = 2
my_DIVIDE = 3
my_DoNothing
End Enum

Dim dblEnter As Double
Dim DoThis As WhatToDo

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
txtDisplay.Text = txtDisplay.Text & 1
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
txtDisplay.Text = txtDisplay.Text & 2
End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
txtDisplay.Text = txtDisplay.Text & 3
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
txtDisplay.Text = txtDisplay.Text & 4
End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
txtDisplay.Text = txtDisplay.Text & 5
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
txtDisplay.Text = txtDisplay.Text & 6
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
txtDisplay.Text = txtDisplay.Text & 7
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
txtDisplay.Text = txtDisplay.Text & 8
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
txtDisplay.Text = txtDisplay.Text & 9
End Sub

Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click
DoThis = WhatToDo.my_ADD
dblEnter = CalculateTheSum(DoThis, Val(txtDisplay.Text))
txtDisplay.Text = ""
End Sub

Private Sub cmdMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
DoThis = WhatToDo.my_SUBTRACT
dblEnter = CalculateTheSum(DoThis, Val(txtDisplay.Text))
txtDisplay.Text = ""
End Sub

Private Sub cmdMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMultiply.Click
DoThis = WhatToDo.my_MULTIPY
dblEnter = CalculateTheSum(DoThis, Val(txtDisplay.Text))
txtDisplay.Text = ""
End Sub

Private Sub myDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myDivide.Click
DoThis = WhatToDo.my_DIVIDE
dblEnter = CalculateTheSum(DoThis, Val(txtDisplay.Text))
txtDisplay.Text = ""
End Sub

Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
dblEnter = CalculateTheSum(DoThis, Val(txtDisplay.Text))
txtDisplay.Text = dblEnter
End Sub

Function CalculateTheSum(ByVal HowToCalculate As WhatToDo, ByVal dblNewVal As Double) As Double
Select Case HowToCalculate
Case WhatToDo.my_ADD
CalculateTheSum = dblEnter + dblNewVal
Case WhatToDo.my_SUBTRACT
CalculateTheSum = dblEnter - dblNewVal
Case WhatToDo.my_MULTIPY
CalculateTheSum = dblEnter * dblNewVal
Case WhatToDo.my_DIVIDE
CalculateTheSum = dblEnter / dblNewVal
End Select
End Function

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
dblEnter = 0
txtDisplay.Text = ""
DoThis = WhatToDo.my_DoNothing
End Sub

  dave_the_red 00:10 10 Dec 05

Sorry for not replying to let you know how things are going with the calculator, havent had time to really look at it since posting but will let you know how i get on.

Thanks

Dave

This thread is now locked and can not be replied to.

What is Amazon Go and will it come to the UK? The store without checkouts or queues

1995-2015: How technology has changed the world in 20 years

Hands-on with the Star Wars fighting drones you can fly yourself

iPhone 9 and beyond: 32 amazing future smartphone developments - graphene, supercapacitor…