• (3 Pages)
  • +
  • 1
  • 2
  • 3

Basic Calculator in VB.Net Rate Topic: **--- 3 Votes

#16 AdamR  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 144
  • Joined: 23-September 08

Posted 23 September 2008 - 09:43 AM

I also had the same problem with the decimals,

http://www.dreaminco...wtopic64803.htm

I know this is months behind, but the above topic will be of help to all those struggling with the same problem. The code and variable names have changed, but following instructions with some adaption will help you fix this problem.
Was This Post Helpful? 0
  • +
  • -

#17 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Posted 23 September 2008 - 05:26 PM

View PostPsychoCoder, on 9 Sep, 2007 - 10:41 AM, said:

So lets take a look at how we code the zero button:

Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
	 'Check the input status
	 If inputStatus Then 'If true
		 'Now check to make sure our
		 'input box has a value
		 If txtInput.Text.Length >= 1 Then
			'Add our zero
			txtInput.Text += cmd0.Text
		 End If
	 End If
End Sub



First we check the status of the inputStatus flag, if its true we know we can enter a number in the box. Here we do a second check, we make sure the length of the text in the input box is at least 1 (it has a value), if so we enter the zero into the input box.


I used this code for the zero button, but every time I click my zero button, I get 2 zeros after a decimal point and after a normal number. I'd like to get only 1 zero when the button is selected if at all possible.

Also, I had tried to get a % button on here as well since the assignment was to imitate the Windows calculator. I've not had any luck with it, but maybe that's just because I'm still all too new in this.

Although a Newb coder, I have managed to make a few assignments on my own so far =). And am proud to say this is only my second ask for help. Below is the code I have so far on the Percent button. Whenever I click the button currently, no matter what number is in the text box, I get a 0. I know I'm doing something wrong, but I'm not exactly sure how to code the correct function of the button.

Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
		If inputStatus Then
			If txtInput.Text >= 0 Then
				txtInput.Text = valHolder1 * 0.01
			End If
		End If
	End Sub



Beside that... I have no idea even where to begin on the M+, MR, MC, MS buttons...=S

Any help would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#18 totallymuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-November 08

Posted 19 November 2008 - 01:14 PM

View PostDarkangler, on 23 Sep, 2008 - 04:26 PM, said:

View PostPsychoCoder, on 9 Sep, 2007 - 10:41 AM, said:

So lets take a look at how we code the zero button:

Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
	 'Check the input status
	 If inputStatus Then 'If true
		 'Now check to make sure our
		 'input box has a value
		 If txtInput.Text.Length >= 1 Then
			'Add our zero
			txtInput.Text += cmd0.Text
		 End If
	 End If
End Sub



First we check the status of the inputStatus flag, if its true we know we can enter a number in the box. Here we do a second check, we make sure the length of the text in the input box is at least 1 (it has a value), if so we enter the zero into the input box.


I used this code for the zero button, but every time I click my zero button, I get 2 zeros after a decimal point and after a normal number. I'd like to get only 1 zero when the button is selected if at all possible.

Also, I had tried to get a % button on here as well since the assignment was to imitate the Windows calculator. I've not had any luck with it, but maybe that's just because I'm still all too new in this.

Although a Newb coder, I have managed to make a few assignments on my own so far =). And am proud to say this is only my second ask for help. Below is the code I have so far on the Percent button. Whenever I click the button currently, no matter what number is in the text box, I get a 0. I know I'm doing something wrong, but I'm not exactly sure how to code the correct function of the button.

Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
		If inputStatus Then
			If txtInput.Text >= 0 Then
				txtInput.Text = valHolder1 * 0.01
			End If
		End If
	End Sub



Beside that... I have no idea even where to begin on the M+, MR, MC, MS buttons...=S

Any help would be greatly appreciated.



Guys its easy whts up with you .. anyways jus correct the code to ...

If Not txtInput.Text = "0" Then
'It met all our requirements so add the zero
txtInput.Text += cmdDecimal.Text
'Toggle the flag to true (only 1 decimal per calculation)
hasDecimal = True
End If
Else
txtInput.Text = "cmdDecimal"
End If
End If
End If
End Sub
#End Region
Was This Post Helpful? 0
  • +
  • -

#19 poknatzky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-December 08

Posted 04 December 2008 - 07:25 AM

hi! i'm just new here nd i jst wnna ask for help coz iam now currntly reviewing for my upcoming exam in visual basic.. our lesson is vb calculator.. since our proctor task us to do a case study in vb. calculator.. stil i dont know what to do.. where to start nd wer to end.. if you have time feel free to chat/pm me in my email account mishz_shzu@yahoo.com thankz.. plz!! i nid youre reply..thanKz
Was This Post Helpful? 0
  • +
  • -

#20 terminator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-March 09

Posted 15 March 2009 - 01:29 PM

View PostPsychoCoder, on 9 Sep, 2007 - 09:41 AM, said:

In this tutoral, Basic Calculator in VB.Net, we will look at creating a basic calculator. The calculator will have the following functionality:
  • Addition
  • Subtraction
  • Division
  • Multiplication
  • Square Root
  • Exponents (Power Of)
  • Clear Entry
  • Clear All
There will be a 2nd tutorial that will cover some more advanced features such as
  • Adding a number to memory
  • Removing a number from memory
  • Calculating with a number in memory
  • Entering numbers by typing
The first thing you need to do is create a new project in Visual Studio (or Visual Basic Express Edition if thats what you use). Once you have created your new project you need to create your user interface, your user interface should look like this:

Attachment attachment

Your user interface will consist of
  • Buttons 0 through 9
  • Buttons for
    • Addition

    • Subtraction

    • Division

    • Multiplication

    • Exponents (x^)

    • Inverse (1/x)

    • Square Root (sqrt)

  • Decimal
  • Equals
  • Backspace
  • CE (Clear Entry)
  • C (Clear All)
  • ReadOnly TextBox for input (Make sure TabStop is also set to False)
How you setup your user interface is up to you, but remember people are used to a calculator looking a certain way so you may wish to follow my example.

In this tutorial I will show you how to code two of the number buttons (since all 10 are the same except the zero button), how to code the calculations buttons, the clear buttons and the backspace buttons. Before writing any code you need to add the following variables to the top (Globals):

'variables to hold operands
Private valHolder1 As Double
Private valHolder2 As Double
'Varible to hold temporary values
Private tmpValue As Double
'True if "." is use else false
Private hasDecimal As Boolean
Private inputStatus As Boolean
Private clearText As Boolean
'variable to hold Operater
Private calcFunc As String



These variables will be used through out our program thats why they're globals. Now, before any calculations can be done, the user needs to be able to enter numbers into the input box, so lets take a look at how to do that (Ill use the number 1 key and the zero key).

Number one key:

Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
	 'Check the inputStatus
	 If inputStatus Then 'Its True
		 'Append values to the value
		 'in the input box
		 txtInput.Text += cmd1.Text
	 Else	'Value is False
		 'Set the value to the value of the button
		 txtInput.Text = cmd1.Text
		 'Toggle inputStatus to True
		 inputStatus = True
	 End If
	End Sub



When a user clicks a number button (in this case the number one button) we check the status of the inputStatus flag. If its true then we know we can just append the next value to the end of whats currently in the input box, otherwise we just enter the number into the input box. All the remaining numbers follow this procedure, except the zero button, this one is slightly different as we don't want the user to be able to enter zero as the first number (this is covered more in the decimal button functionality).

So lets take a look at how we code the zero button:

Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
	 'Check the input status
	 If inputStatus Then 'If true
		 'Now check to make sure our
		 'input box has a value
		 If txtInput.Text.Length >= 1 Then
			'Add our zero
			txtInput.Text += cmd0.Text
		 End If
	 End If
End Sub



First we check the status of the inputStatus flag, if its true we know we can enter a number in the box. Here we do a second check, we make sure the length of the text in the input box is at least 1 (it has a value), if so we enter the zero into the input box.

For adding a decimal to our input box we need to first make sure our input box doesn't already contain one, then we need to make sure our input box has a value (don't want the user to be able to enter a decimal as the first value). Then we make sure the value in the input area isn't 0 (zero), this we will handle later.

If all those are true then we enter the decimal then toggle the hasDecimal to True, so the user cant enter a 2nd one. Now, if the input area doesn't have a value, we enter 0., as we assume the user is wanting to work with a decimal value such as 0.5. Lets take a look at the procedure for doing this:

Private Sub cmdDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecimal.Click
		'Check for input status (we want true)
   If inputStatus Then
	   'Check if it already has a decimal (if it does then do nothing)
	   If Not hasDecimal Then
		   'Check to make sure the length is > than 1
		   'Dont want user to add decimal as first character
		   If txtInput.Text.Length > 1 Then
				'Make sure 0 isnt the first number
				If Not txtInput.Text = "0" Then
					'It met all our requirements so add the zero
					txtInput.Text += cmdDecimal.Text
					'Toggle the flag to true (only 1 decimal per calculation)
					hasDecimal = True
				End If
		  Else
			   'Since the length isnt > 1
			   'make the text 0.
			   txtInput.Text = "0."
		  End If
	   End If
   End If
End Sub



As you can see, we check all the items mentioned above, if they're True we add the decimal, otherwise we add 0. to the input area.

Next we want to be able to add numbers together. The first thing we do here is to make sure the input box has a value (Length > 1). If it does then we check the calcFunc value. The calcFunction variable will be used to tell our CalculateTotals procedure which calculation to perform. Here, if the value is empty (String.Empty) we assign the value of our input box to a variable, valHolder1, which will hold the first part of all calculations, then clear out the input box so the user can enter a 2nd number.

If the calcFunc variable isnt empty then we call our CalculateTotals procedure to display a total to the user. We then assign the value of Add to our variable for the next turn through, then we toggle the bb]hasDecimal[/b] flag to False.

Now lets take a look at how we accomplished this:

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
   'Make sure out input box has a value
   If txtInput.Text.Length <> 0 Then
	   'Check the value of our function flag
	   If calcFunc = String.Empty Then 'Flag is empty
		   'Assign the value in our input
		   'box to our holder
		   valHolder1 = CType(txtInput.Text, Double)
		   'Empty the input box
		   txtInput.Text = String.Empty
	   Else 'Flag isnt empty
		   'Call our calculate totals method
		   CalculateTotals()
	   End If
	   'Assign a value to our calc function flag
	   calcFunc = "Add"
	   'Toggle the decimal flag
	   hasDecimal = False
   End If
End Sub



Believe it or not, all the other basic calculation buttons are the same as the Add button, with the exception of what we set calcFunc to. In the other buttons we set this variable to the calculation we want to perform, Subtract,
Divide, Multiply, and so on, so there really isn't a reason to show how that is done since we did the Add button and the others are the same.

Lets say you want to give the user the option to calculation Exponents, 4^2 for example. To code this button you need a couple of checks before doing anything. First we need to check and make sure the input area has a value, if it does then we check to see the value of the calcFunc variable.

If this is empty, we then convert the value of the input area to a Double and assign it to the valHolder1 variable to hold on to, this will be used for the calculations in the CalculateTotals procedure and empth the value from the input area.. If its not empty we directly call the CalculateTotals function as this means the user has already entered 2 numbers.

We then assign the value of PowerOf to our calcFunc variable, this will tell CalculateTotals what calculation to perform, and toggle the hasDecimal flag to False.

Lets take a look at how we accomplished all of this:

Private Sub cmdPowerOf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPowerOf.Click
   'Make sure the input box has a value
   If txtInput.Text.Length <> 0 Then
	   'Check if the calcFunc flag is empty
	   If calcFunc = String.Empty Then
		   'Assign the value of the input box to our variable
		   valHolder1 = CType(txtInput.Text, Double)
		   'Empty the input box
		   'So the user can enter the power of value
		   txtInput.Text = String.Empty
	   Else
		   'Call the calculate totals method
		   CalculateTotals()
	   End If
	   'Assign our flag the value of "PowerOf"
	   calcFunc = "PowerOf"
	   'Reset the decimal flag
	   hasDecimal = False
   End If
End Sub



Doing a Square Root is somewhat different as it doesn't take 2 values, just the number you want the square root of, so some of the checking required in the other calculations isn't required here. For a Square Root we first check to ensure the input area has a value. If it does have a value we assign the value of the input area, converted to a Double, to our tmpValue variable.

Once we have the value, we call the System.Math.Sqrt Method to perform the calculations on the tmpValue variable. Once this is complete we assign the resulting value to our input area, then toggle the hasDecimal flag to False.

Lets take a look at how this is done:

Private Sub cmdSqrRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSqrRoot.Click
   'Make sure the input box has a value
   If txtInput.Text.Length <> 0 Then
	   'Assign our variable the value in the input box
	   tmpValue = CType(txtInput.Text, Double)
	   'Perform the square root
	   tmpValue = System.Math.Sqrt(tmpValue)
	   'Display the results in the input box
	   txtInput.Text = CType(tmpValue, String)
	   'Clear the decimal flag
	   hasDecimal = False
   End If
End Sub



The Equals button is quite simple. Here, we first check to make sure our input area has a value and that our valHolder1 variable isn't a zero (Divide by 0 is a bad thing). If both of these are true we call the CalculateTotals procedure to perform our calculations based on the value of the calcFunc flag. We then clear the value of calcFunc and toggle the hasDecimal flag to False. This is done like this:

Private Sub cmdEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEqual.Click
   'Make sure theres a value in the input box
   'And that our temp value isnt 0
   If txtInput.Text.Length <> 0 AndAlso valHolder1 <> 0 Then
	   'Call the calculate totals method
	   CalculateTotals()
	   'Clear the calcFunction value
	   calcFunc = String.Empty
	   'Toggle the decimal flag
	   hasDecimal = False
   End If
End Sub



We have 3 more buttons to look at before we look at the CalculateTotals procedure. First we'll look at the backspace button.For the backspace, first we need to make sure the input are has a value. If it does then we retrieve the next to last character and see if its a decimal, if it is we toggle the hasDecimal flag to False. Next we create an Integer variable (loc) to hold the length of the contents in the input area. From there we use Remove, along with loc to remove the last character of the string for each time the user clicks the backspace button.

Private Sub cmdBackspace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackspace.Click
   'Declare locals needed
   Dim str As String
   Dim loc As Integer
   'Make sure the text length is > 1
   If txtInput.Text.Length > 0 Then
	   'Get the next to last character
	   str = txtInput.Text.Chars(txtInput.Text.Length - 1)
	   'Check if its a decimal
	   If str = "." Then
		   'If it is toggle the hasDecimal flag
		   hasDecimal = False
	   End If
	   'Get the length of the string
	   loc = txtInput.Text.Length
	   'Remove the last character, incrementing by 1
	   txtInput.Text = txtInput.Text.Remove(loc - 1, 1)
   End If
End Sub



The last 2 buttons I'm going to demonstrate are the CE (Clear entry) and C (Clear all) buttons. These are very simple. First the clear entry button. What we do here is set the value in the input area to empty (String.Empty), and the hasDecimal flag to false.

Private Sub cmdClearEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearEntry.Click
   'Empty the input box
	txtInput.Text = String.Empty
	'Toggle the decimal flag
	hasDecimal = False
End Sub



The clear all button required a bit more code as we do more with this button. Here we set our 2 holder variables, valHolder1 and valHolder2 to 0 (zero), we then set the calcFunc flag to String.Empty and the hasDecimal flag to False, like this:

Private Sub cmdClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearAll.Click
   'Empty the text in the input box
   txtInput.Text = String.Empty
   'Clear out both temp values
   valHolder1 = 0
   valHolder2 = 0
   'Set the calc switch to empty
   calcFunc = String.Empty
   'Toggle the hasDecimal flag
   hasDecimal = False
End Sub



Those are the buttons you need for a Basic calculator. The final thing we're going to look at is the procedure that actually does the calculations, CalculateTotals. Here the first thing we do is set our variable valHolder2 to the current value of the input area. We then do a Select Case on the value of calcFunc so we know which calculations to perform. We perform our calculations (add, subtract, divide, multiply, exponent, etc) and set the results to the input area so the user can see their results. Finally we set the inputEntry flag to False. THis iw hat this procedure looks like:

Private Sub CalculateTotals()
   valHolder2 = CType(txtInput.Text, Double)
   Select Case calcFunc
	   Case "Add"
		   valHolder1 = valHolder1 + valHolder2
	   Case "Subtract"
		   valHolder1 = valHolder1 - valHolder2
	   Case "Divide"
		   valHolder1 = valHolder1 / valHolder2
	   Case "Multiply"
		   valHolder1 = valHolder1 * valHolder2
	   Case "PowerOf"
		   valHolder1 = System.Math.Pow(valHolder1, valHolder2)
   End Select
   txtInput.Text = CType(valHolder1, String)
   inputStatus = False
End Sub



NOTE: For the Exponents (Power Of) we use the System.Math.Pow Method for calculating the value.

Thats it, thats how you create a basic calculator in VB.Net. I hope you find this tutorial helpful. I am including the project file with this tutorial, but remember this solution is under the GNU GENERAL PUBLIC LICENSE so you may not remove the header from the files or turn this project in as your homework assignment.

I know I am forced to go with the honor system in this, but if you do just turn this in as your assignment not only will you be cheating, but you will learn nothing, and subsequently wont know enough to become a programmer once you get out of school.

I will be doing a 2nd part to this tutorial where I look at adding more advanced functionality to this calculator, such
as adding a number to memory, removing a number from memory, calculations with a number in memory and more. Also, I will be creating a C# version of this calculator for the C# users.

Thank you for reading!

Attachment attachment



I enjoyed your tutorial!

My solution to the cmdDecimal code is:

[code] 'Check for input status (we want true)
If inputStatus Then
'Check if it already has a decimal (if it does then do nothing)
If Not hasDecimal Then
'Two if statements have been nullified by moving the Else statement to the same level as the first If
'Check to make sure the length is > than 1, although inputStatus = True already says so
'Dont want user to add decimal as first character
'If txtInput.Text.Length > 1 Then
'Make sure 0 isnt the first number, although this should not be possible
'If Not txtInput.Text = "0" Then
'It met all our requirements so add the decimal
txtInput.Text += cmdDecimal.Text
'Toggle the flag hasDecimal to true (only 1 decimal per calculation)
hasDecimal = True
'End If
'The Else statement was here originally
'This is an example of a logical error
End If
'End If
Else
'Since the length isnt > 1
'make the text 0.
txtInput.Text = "0."
'The following three lines have been added by me
'Toggle the flags hasDecimal and inputStatus to True
hasDecimal = True
inputStatus = True
End If
[code]
Yours sincerely,

Terminator
Was This Post Helpful? 0
  • +
  • -

#21 jennxoxo  Icon User is offline

  • New D.I.C Head

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

Posted 13 April 2009 - 03:57 PM

did the second tutorial ever come out??
Was This Post Helpful? 0
  • +
  • -

#22 phoeniX_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-August 09

Posted 05 August 2009 - 02:32 AM

great tut, thx! :)
Was This Post Helpful? 0
  • +
  • -

#23 Umaid123  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 15-December 08

Posted 12 August 2009 - 09:37 AM

Really Thanx for Baackspace. It really works.
:^: :^: :^:
Was This Post Helpful? 0
  • +
  • -

#24 Stetlo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 09

Posted 12 October 2009 - 03:20 PM

I get an error for CalculateTotals saying that it's not declared. Help please.
Was This Post Helpful? 0
  • +
  • -

#25 EnvXOwner  Icon User is offline

  • PHP Brogrammer
  • member icon

Reputation: 355
  • View blog
  • Posts: 2,314
  • Joined: 10-August 09

Posted 29 November 2009 - 10:00 PM

You guys are really idiotic the code for the decimal button is
'Check for input status (we want true)
		If inputStatus Then
			'Check if it already has a decimal (if it does then do nothing)
			If Not hasDecimal Then
				'It met all our requirements so add the zero
				txtInput.Text += btnDecimal.Text
				'Toggle the flag to true (only 1 decimal per calculation)
				hasDecimal = True
				If txtInput.Text = ("0") Then
					txtInput.Text = ("0" + btnDecimal.Text)
				End If
			End If
		End If

Was This Post Helpful? 0
  • +
  • -

#26 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 1953
  • View blog
  • Posts: 8,687
  • Joined: 29-May 08

Posted 29 December 2009 - 10:40 PM

This tutorial was helpful in me creating calculator using vb.net code.
Was This Post Helpful? 0
  • +
  • -

#27 vbdotnet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 07-December 09

Posted 03 January 2010 - 09:26 PM

thanl you!
Was This Post Helpful? 0
  • +
  • -

#28 Guest_anseas*


Reputation:

Posted 10 May 2010 - 09:49 AM

Re: Basic Calculator in VB.Net

Posted 25 July 2008 - 03:11 PM
You did use the CalculateTotals method included in the tutorial correct?

Private Sub CalculateTotals()
valHolder2 = CType(txtInput.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
End Select
txtInput.Text = CType(valHolder1, String)
inputStatus = False
End Sub


Was This Post Helpful? 0

* +
* -

* Back to top
* Reply Icon MultiQuote
* Reply Icon Reply

#14 areyouon Icon User is offline

*
* New D.I.C Head
* Pip


Reputation: 0

* View blog

* Posts: 3
* Joined: 25-July 08


Dream Kudos: 0
Re: Basic Calculator in VB.Net

Posted 26 July 2008 - 10:06 AM
I should've finished the tutorial before getting frustrated. I hadn't reached that part yet. I should know better than to try writing code after 5pm!!! Brain doesn't work as well.

I'm still having an issue with the "0" and "0." portion but I'll wait until all is finished before seeing if I need any further help.

Thanks for the reply!!!


View PostPsychoCoder, on 25 Jul, 2008 - 04:11 PM, said:
You did use the CalculateTotals method included in the tutorial correct?

Private Sub CalculateTotals()
valHolder2 = CType(txtInput.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
End Select
txtInput.Text = CType(valHolder1, String)
inputStatus = False
End Sub


Was This Post Helpful? 0

* +
* -

* Back to top
* Reply Icon MultiQuote
* Reply Icon Reply

#15 areyouon Icon User is offline

*
* New D.I.C Head
* Pip


Reputation: 0

* View blog

* Posts: 3
* Joined: 25-July 08


Dream Kudos: 0
Re: Basic Calculator in VB.Net

Posted 26 July 2008 - 12:09 PM
I'm still having an issue with the decimal button. Upon load it won't show (or acknowledge) "0." but will work if I enter any other number first and then press decimal. If I then hit Clear or CE I can then press decimal and "0." appears as first character. I thought maybe upon load something was telling the application "hasDecimal=True" but can't find any indication of that. Besides, if that were happening I couldn't add a decimal after entering a number.

I've rechecked my code but can't find any errors either. Any ideas?

Other than this, all works fine... Great tutorial!

Here's my decimal btn code for reference:

Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click

'Check input status and limit to one decimal with "0" first
If inputStatus Then
If Not hasDecimal Then
If Me.txtDisplay.Text.Length > 0 Then
If Not Me.txtDisplay.Text = "0" Then
Me.txtDisplay.Text += Me.btnDecimal.Text
hasDecimal = True 'Turns decimal limiter on
End If
Else
'since length not > 1 make text "0."
Me.txtDisplay.Text = "0."
End If
End If
End If

End Sub





View Postareyouon, on 26 Jul, 2008 - 11:06 AM, said:
I should've finished the tutorial before getting frustrated. I hadn't reached that part yet. I should know better than to try writing code after 5pm!!! Brain doesn't work as well.

I'm still having an issue with the "0" and "0." portion but I'll wait until all is finished before seeing if I need any further help.

Thanks for the reply!!!


View PostPsychoCoder, on 25 Jul, 2008 - 04:11 PM, said:
You did use the CalculateTotals method included in the tutorial correct?

Private Sub CalculateTotals()
valHolder2 = CType(txtInput.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
End Select
txtInput.Text = CType(valHolder1, String)
inputStatus = False
End Sub
Was This Post Helpful? 0

#29 EnvXOwner  Icon User is offline

  • PHP Brogrammer
  • member icon

Reputation: 355
  • View blog
  • Posts: 2,314
  • Joined: 10-August 09

Posted 13 May 2010 - 05:49 PM

View Postanseas, on 10 May 2010 - 10:49 AM, said:

Re: Basic Calculator in VB.Net

Posted 25 July 2008 - 03:11 PM
You did use the CalculateTotals method included in the tutorial correct?

Private Sub CalculateTotals()
valHolder2 = CType(txtInput.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
End Select
txtInput.Text = CType(valHolder1, String)
inputStatus = False
End Sub


Was This Post Helpful? 0

* +
* -

* Back to top
* Reply Icon MultiQuote
* Reply Icon Reply

#14 areyouon Icon User is offline

*
* New D.I.C Head
* Pip


Reputation: 0

* View blog

* Posts: 3
* Joined: 25-July 08


Dream Kudos: 0
Re: Basic Calculator in VB.Net

Posted 26 July 2008 - 10:06 AM
I should've finished the tutorial before getting frustrated. I hadn't reached that part yet. I should know better than to try writing code after 5pm!!! Brain doesn't work as well.

I'm still having an issue with the "0" and "0." portion but I'll wait until all is finished before seeing if I need any further help.

Thanks for the reply!!!


View PostPsychoCoder, on 25 Jul, 2008 - 04:11 PM, said:
You did use the CalculateTotals method included in the tutorial correct?

Private Sub CalculateTotals()
valHolder2 = CType(txtInput.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
End Select
txtInput.Text = CType(valHolder1, String)
inputStatus = False
End Sub


Was This Post Helpful? 0

* +
* -

* Back to top
* Reply Icon MultiQuote
* Reply Icon Reply

#15 areyouon Icon User is offline

*
* New D.I.C Head
* Pip


Reputation: 0

* View blog

* Posts: 3
* Joined: 25-July 08


Dream Kudos: 0
Re: Basic Calculator in VB.Net

Posted 26 July 2008 - 12:09 PM
I'm still having an issue with the decimal button. Upon load it won't show (or acknowledge) "0." but will work if I enter any other number first and then press decimal. If I then hit Clear or CE I can then press decimal and "0." appears as first character. I thought maybe upon load something was telling the application "hasDecimal=True" but can't find any indication of that. Besides, if that were happening I couldn't add a decimal after entering a number.

I've rechecked my code but can't find any errors either. Any ideas?

Other than this, all works fine... Great tutorial!

Here's my decimal btn code for reference:

Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click

'Check input status and limit to one decimal with "0" first
If inputStatus Then
If Not hasDecimal Then
If Me.txtDisplay.Text.Length > 0 Then
If Not Me.txtDisplay.Text = "0" Then
Me.txtDisplay.Text += Me.btnDecimal.Text
hasDecimal = True 'Turns decimal limiter on
End If
Else
'since length not > 1 make text "0."
Me.txtDisplay.Text = "0."
End If
End If
End If

End Sub





View Postareyouon, on 26 Jul, 2008 - 11:06 AM, said:
I should've finished the tutorial before getting frustrated. I hadn't reached that part yet. I should know better than to try writing code after 5pm!!! Brain doesn't work as well.

I'm still having an issue with the "0" and "0." portion but I'll wait until all is finished before seeing if I need any further help.

Thanks for the reply!!!


View PostPsychoCoder, on 25 Jul, 2008 - 04:11 PM, said:
You did use the CalculateTotals method included in the tutorial correct?

Private Sub CalculateTotals()
valHolder2 = CType(txtInput.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
End Select
txtInput.Text = CType(valHolder1, String)
inputStatus = False
End Sub

I don't know who you are, but if you post on here again please don't do that. It is plan out ugly. Thank you.
Was This Post Helpful? 0
  • +
  • -

#30 EnvXOwner  Icon User is offline

  • PHP Brogrammer
  • member icon

Reputation: 355
  • View blog
  • Posts: 2,314
  • Joined: 10-August 09

Posted 18 May 2010 - 02:10 PM

Real mature.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3