Stats: 3,190,101 members, 7,939,447 topics. Date: Wednesday, 04 September 2024 at 05:41 AM |
Nairaland Forum / Science/Technology / Programming / Vb.net Calculator (5964 Views)
How To Make A Simple Calculator In Notepad Using .bat Format / I Need A Help To Write A Simple Login Form Using Vb.net / Post Ur Vb 6.0 Questions Here (2) (3) (4)
Vb.net Calculator by 2legit2qwt: 1:29am On Jun 06, 2011 |
I am new to vb.net and i'm needing help with this small calculator program and will appreciate whoever can help me out. I am able to write the program to do the calculations right but my instructor specifically wants me to use a single event handler to handle all the operations like +,-,* and even = signs. It would have been easier for me if this was java because that's what i'm used to but not vb. Below is my code so far, Only the addition works now and i need to include codes for the other calculations using the same even handler i.e operations_click. some functions are not working but they should not affect the overall performance of the program. Please ignore the EqualsButton_Click, i'm the one using that to test first, it should be included in the Operations_click method and not stand alone. Thanks guys Public Class CalculatorForm Private FirstNumberInput As Double = 0 Private LastNumberInput As Double = 0 Private OperationCheck As Boolean = False Private Operation As String Private Sub OneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OneButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "1" Else ResultLabel.Text = ResultLabel.Text & OneButton.Text 'Appends the number "1" to user entry End If End Sub Private Sub TwoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TwoButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "2" Else ResultLabel.Text = ResultLabel.Text & TwoButton.Text 'Appends the number "2" to user entry End If End Sub Private Sub ThreeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThreeButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "3" Else ResultLabel.Text = ResultLabel.Text & ThreeButton.Text 'Appends the number "3" to user entry End If End Sub Private Sub FourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FourButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "4" Else ResultLabel.Text = ResultLabel.Text & FourButton.Text 'Appends the number "4" to user entry End If End Sub Private Sub FiveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiveButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "5" Else ResultLabel.Text = ResultLabel.Text & FiveButton.Text 'Appends the number "5" to user entry End If End Sub Private Sub SixButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SixButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "6" Else ResultLabel.Text = ResultLabel.Text & SixButton.Text 'Appends the number "6" to user entry End If End Sub Private Sub SevenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SevenButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "7" Else ResultLabel.Text = ResultLabel.Text & SevenButton.Text 'Appends the number "7" to user entry End If End Sub Private Sub EightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EightButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "8" Else ResultLabel.Text = ResultLabel.Text & EightButton.Text 'Appends the number "8" to user entry End If End Sub Private Sub NineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NineButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "9" Else ResultLabel.Text = ResultLabel.Text & NineButton.Text 'Appends the number "9" to user entry End If End Sub Private Sub ZeroButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZeroButton.Click If ResultLabel.Text = "0" Then ResultLabel.Text = "0" Else ResultLabel.Text = ResultLabel.Text & ZeroButton.Text 'Appends the number "0" to user entry End If End Sub Private Sub DecimalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DecimalButton.Click ResultLabel.Text = DecimalButton.Text & "" 'clears the screen End Sub Private Sub Operations_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusButton.Click, MinusButton.Click, MultiplyButton.Click, DivideButton.Click FirstNumberInput = ResultLabel.Text ResultLabel.Text = "0" Operation = "+" End Sub Private Sub BackSpaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackSpaceButton.Click, ClearEntryButton.Click, AbortButton.Click ResultLabel.Text = "0" End Sub Private Sub ResultLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResultLabel.Click ResultLabel.Text = 0 End Sub Private Sub EqualsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EqualsButton.Click Dim Result As Double LastNumberInput = ResultLabel.Text If Operation = "+" Then Result = FirstNumberInput + LastNumberInput ElseIf Operation = "-" Then Result = FirstNumberInput - LastNumberInput End If FirstNumberInput = Result ResultLabel.Text = Result End Sub End Class |
Re: Vb.net Calculator by dellnet: 3:58pm On Jun 06, 2011 |
When you say single event handler, I don't know if there is a standard to map all events in VB.NET application to one handler, I have never come across such. Unless you mean map to a single function or sub. |
Re: Vb.net Calculator by 2legit2qwt: 2:53am On Jun 07, 2011 |
I meant a single even handler to handle the click events for all the operations +,-,*/ and /. Thanks for the response |
Re: Vb.net Calculator by dellnet: 3:36am On Jun 08, 2011 |
If you click any of the arithmetic signs is this sub executed? Private Sub Operations_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusButton.Click, MinusButton.Click, MultiplyButton.Click, DivideButton.Click |
Re: Vb.net Calculator by TRed: 3:34pm On Jun 09, 2011 |
this will work Private Sub Operations_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusButton.Click, MinusButton.Click, MultiplyButton.Click, DivideButton.Click FirstNumberInput = resultlabel.Text resultlabel.Text = "0" If PlusButton.Focused Then Operation = 1 Else If MinusButton.Focused Then Operation = 2 Else If DivideButton.Focused Then Operation = 3 Else If MultiplyButton.Focused Then Operation = 4 End If End If End If End If End Sub Private Sub equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equal.Click Dim Result As Double LastNumberInput = resultlabel.Text If Operation = 1 Then Result = FirstNumberInput + LastNumberInput Else If Operation = 2 Then Result = FirstNumberInput - LastNumberInput End If If Operation = 3 Then Result = FirstNumberInput / LastNumberInput Else If Operation = 4 Then Result = FirstNumberInput * LastNumberInput End If End If End If FirstNumberInput = Result resultlabel.Text = Result End Sub |
Re: Vb.net Calculator by 2legit2qwt: 11:26pm On Jun 11, 2011 |
Hi guys, Thanks for your help but fortunately for me, i was able to figure how to make everything work which is why I have not been able to check for any update on here. dellnet and T.Red i'm grateful to you both for willing to help. |
(1) (Reply)
Tutorial: How To Return Html In Json Response Using Asp.net And Jquery / IQ And Programming... Does Having A High IQ Make You A Better Programmer? / My Data Analytics Journey
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39 |