vb calculator

memory button help

Page 1 of 1

8 Replies - 4945 Views - Last Post: 10 August 2012 - 06:46 AM Rate Topic: -----

#1 jennxoxo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 09

vb calculator

Posted 13 April 2009 - 06:34 PM

I can not for the life of me figure out the memory buttons. I have never used them and am pretty sure I'm not even clear on what they actually do....Please. Help.
I'm posting my entire code but its just the last three events towards the bottom. I don't know if they are coded right, if they work, how they work, etc. Corrections or help please?
Public Class Calculator

	Private variable1 As Double
	Private variable2 As Double
	Private tmpValue As Double
	Private hasDecimal As Boolean
	Private inputStatus As Boolean
	Private clearText As Boolean
	Private calcFunc As String
	Private memory As Double

	Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
		If inputStatus Then
			If txtDisplay.Text.Length >= 0 Then
				txtDisplay.Text += btn0.Text
			End If
		End If
	End Sub

	Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
		If inputStatus Then
			txtDisplay.Text += btn1.Text
		Else
			txtDisplay.Text = btn1.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
		If inputStatus Then
			txtDisplay.Text += btn2.Text
		Else
			txtDisplay.Text = btn2.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
		If inputStatus Then
			txtDisplay.Text += btn3.Text
		Else
			txtDisplay.Text = btn3.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
		If inputStatus Then
			txtDisplay.Text += btn4.Text
		Else
			txtDisplay.Text = btn4.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
		If inputStatus Then
			txtDisplay.Text += btn5.Text
		Else
			txtDisplay.Text = btn5.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
		If inputStatus Then
			txtDisplay.Text += btn6.Text
		Else
			txtDisplay.Text = btn6.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
		If inputStatus Then
			txtDisplay.Text += btn7.Text
		Else
			txtDisplay.Text = btn7.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
		If inputStatus Then
			txtDisplay.Text += btn8.Text
		Else
			txtDisplay.Text = btn8.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
		If inputStatus Then
			txtDisplay.Text += btn9.Text
		Else
			txtDisplay.Text = btn9.Text
			inputStatus = True
		End If
	End Sub

	Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
		If inputStatus Then
			If Not hasDecimal Then
				If txtDisplay.Text.Length > 0 Then
					If Not txtDisplay.Text = "0" Then
						txtDisplay.Text += btnDot.Text
						hasDecimal = True
					End If
				Else
					txtDisplay.Text = "0."
				End If
			End If
		End If
	End Sub

	Private Sub CalculateTotals()
		variable2 = CType(txtDisplay.Text, Double)
		Select Case calcFunc
			Case "Add"
				variable1 = variable1 + variable2
			Case "Subtract"
				variable1 = variable1 - variable2
			Case "Divide"
				variable1 = variable1 / variable2
			Case "Multiply"
				variable1 = variable1 * variable2
		End Select
		txtDisplay.Text = CType(variable1, String)
		inputStatus = False
	End Sub

	Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
		If txtDisplay.Text.Length <> 0 Then
			If calcFunc = String.Empty Then
				variable1 = CType(txtDisplay.Text, Double)
				txtDisplay.Text = String.Empty
			Else
				CalculateTotals()
			End If
			calcFunc = "Add"
			hasDecimal = False
		End If
	End Sub

	Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
		If txtDisplay.Text.Length <> 0 Then
			If calcFunc = String.Empty Then
				variable1 = CType(txtDisplay.Text, Double)
				txtDisplay.Text = String.Empty
			Else
				CalculateTotals()
			End If
			calcFunc = "Subtract"
			hasDecimal = False
		End If
	End Sub

	Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
		If txtDisplay.Text.Length <> 0 Then
			If calcFunc = String.Empty Then
				variable1 = CType(txtDisplay.Text, Double)
				txtDisplay.Text = String.Empty
			Else
				CalculateTotals()
			End If
			calcFunc = "Divide"
			hasDecimal = False
		End If
	End Sub

	Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
		If txtDisplay.Text.Length <> 0 Then
			If calcFunc = String.Empty Then
				variable1 = CType(txtDisplay.Text, Double)
				txtDisplay.Text = String.Empty
			Else
				CalculateTotals()
			End If
			calcFunc = "Multiply"
			hasDecimal = False
		End If
	End Sub

	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
		If txtDisplay.Text.Length <> 0 AndAlso variable1 <> 0 Then
			CalculateTotals()
			calcFunc = String.Empty
			hasDecimal = False
		End If
	End Sub

	Private Sub btnBackspace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackspace.Click
		Dim str As String
		Dim loc As Integer
		If txtDisplay.Text.Length > 0 Then
			str = txtDisplay.Text.Chars(txtDisplay.Text.Length - 1)
			If str = "." Then
				hasDecimal = False
			End If
			loc = txtDisplay.Text.Length
			txtDisplay.Text = txtDisplay.Text.Remove(loc - 1, 1)
		End If
	End Sub

	Private Sub btnClearEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearEntry.Click
		txtDisplay.Text = String.Empty
		hasDecimal = False
	End Sub

	Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
		txtDisplay.Text = String.Empty
		variable1 = 0
		variable2 = 0
		calcFunc = String.Empty
		hasDecimal = False
	End Sub

	Private Sub btnPlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlusMinus.Click
		If inputStatus Then
			If txtDisplay.Text = -txtDisplay.Text Then
				txtDisplay.Text = +txtDisplay.Text
			ElseIf txtDisplay.Text = +txtDisplay.Text Then
				txtDisplay.Text = -txtDisplay.Text
			End If
		End If
	End Sub

	Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
		If txtDisplay.Text.Length <> 0 Then
			tmpValue = CDbl(txtDisplay.Text)
			tmpValue = System.Math.Sqrt(tmpValue)
			txtDisplay.Text = CStr(tmpValue)
			hasDecimal = False
		End If
	End Sub

	Private Sub btnInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInverse.Click
		If txtDisplay.Text.Length <> 0 Then
			tmpValue = CDbl(txtDisplay.Text)
			tmpValue = 1 / tmpValue
			txtDisplay.Text = CStr(tmpValue)
			hasDecimal = False
		End If
	End Sub

	Private Sub btnMemoryRecall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryRecall.Click
		If txtbox1.Text = "M" Then
			txtDisplay.Text = CStr(Memory)
			inputStatus = True
		End If
	End Sub

	Private Sub btnMemoryPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryPlus.Click
		If txtDisplay.Text.Length > 0 Then
			memory = memory + CDbl(txtDisplay.Text)
			txtbox1.Text = "M"
		End If
	End Sub

	Private Sub btnMemoryClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryClear.Click
		If txtDisplay.Text.Length > 0 Then
			memory = memory - CDbl(txtDisplay.Text)
			txtbox1.Text = "M"
		End If
	End Sub

	Private Sub btnMemoryStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryStore.Click
		If inputStatus() = True Then memory = CDbl(txtDisplay.Text)
		If memory <> 0 Then
			txtbox1.Text = "M"
		Else
			txtbox1.Text = Nothing
		End If
	End Sub
End Class

This post has been edited by jennxoxo: 13 April 2009 - 06:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: vb calculator

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: vb calculator

Posted 13 April 2009 - 07:15 PM

What do you want to happen when the buttons are clicked? Right now two of them might set the value of a variable called 'memory' and display some value in a textbox. The third button uses the value of memory as a condition in an If Then structure. Depending on the value, a textbox will display either "M" or remain blank. Code looks good so are you having any problem? Error messages or nothing happens? Use step debug to see what happens as each line executes.

How is the value of variable inputStatus set? Never mind, I see now. It starts as False and then changes only if a condition is met later.

A debug technique is to have something change that you can see no matter which decision path is taken, display a value to a label or textbox or print directly to form. After debugging, you can eliminated the test code. So instead of nothing have the textbox say "no change" and revert to original when confident code is working.

This post has been edited by June7: 13 April 2009 - 07:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 jennxoxo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 09

Re: vb calculator

Posted 13 April 2009 - 07:52 PM

Well I am looking for them to do their normal operations, I just am not clear as to how to code for them because I am not very familiar with those buttons on the calculator.
Was This Post Helpful? 0
  • +
  • -

#4 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: vb calculator

Posted 13 April 2009 - 10:06 PM

You are building a calculator, right? The memory buttons will do whatever you code them to do. So if you want to emulate a real world calculator, sit down with the operators manual of one and find out what they normally do. It looks like your code will act as a calculator. So, does it? It's not for us say if you have met the instructor's requirements. Test! Debug! Test! Debug! Test! till it works.

This post has been edited by June7: 13 April 2009 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 jennxoxo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 09

Re: vb calculator

Posted 14 April 2009 - 09:23 PM

(for calculator similar to windows calc)
OK, I figured out the first three of the memory keys and got them all working properly but my memory store is throwing something strange and I really don't know how to fix it.

 Private Sub btnMemoryRecall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryRecall.Click
		If txtbox1.Text = "M" Then
			txtDisplay.Text = CStr(Memory)
			inputStatus = True
		End If
	End Sub

	Private Sub btnMemoryPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryPlus.Click
		If txtDisplay.Text.Length > 0 Then
			memory = memory + CDbl(txtDisplay.Text)
			txtbox1.Text = "M"
		End If
	End Sub

	Private Sub btnMemoryClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryClear.Click
		If txtDisplay.Text.Length > 0 Then
			memory = memory - CDbl(txtDisplay.Text)
			txtbox1.Text = " "
		End If
	End Sub

	Private Sub btnMemoryStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemoryStore.Click
		If inputStatus = True Then memory = CDbl(txtDisplay.Text)
		If memory <> 0 Then
			txtbox1.Text = "M"
		Else
			txtbox1.Text = Nothing
		End If
	End Sub

End Class


It is highlighting memory=Cdbl(txtDisplay.Text) and saying: Invalid cast exception. Conversion from string "" to type 'Double' is not valid.

Help. :( I'm so close!

This post has been edited by jennxoxo: 14 April 2009 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: vb calculator

Posted 15 April 2009 - 05:47 PM

Congratulations on the progress!

I do not program in VB.Net, just VB/VBA. So have to ask: Is it really necessary to convert the textbox value? In VBA setting a numeric data type variable equal to a textbox is straightforward. As long as there are only digits in the value, no problem. Otherwise, I can't see anything else to check out. Your code structure looks really good. Sorry.

This post has been edited by June7: 15 April 2009 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 DataPriest  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 57
  • Joined: 29-April 09

Re: vb calculator

Posted 30 April 2009 - 04:35 AM

I have attached BasiCalc.zip (224KB) which I hope contains code that will hopefully shed a light and answer most of your questions.

This is a more like Windows calculator in Normal mode but a bit extended. I've recently finished developing it and it works like a charm. You just have to love it.

If you have any queries concerning Basic Calculator 1.0, feel free to email me directly on mrushizha@aacs.co.zw

Thanks.

Attached Image

Attached File(s)


This post has been edited by DataPriest: 30 April 2009 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 KitLee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 12

Re: vb calculator

Posted 10 August 2012 - 06:24 AM

your calculator you solve already??
Was This Post Helpful? 0
  • +
  • -

#9 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: vb calculator

Posted 10 August 2012 - 06:46 AM

You should just convert textDisplay.text into a number and assign it to a variable.
Like:

dim dblTextDisplay as double
double.tryparse(textDisplay.text, dblTextDisplay)


Then you can substitute your Cdbl command with a variable.
I believe this would help you avoid the cast issue.




Crap.. just realized how old this thread was..sorry.

This post has been edited by torind_2000: 10 August 2012 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1