-
Jul 22nd, 2010, 08:07 PM
#1
-
Jul 23rd, 2010, 11:51 AM
#2
Re: calculator
here's an upgrade, to allow keyboard input.
set form1.keypreview = true, then add this:
vb Code:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Dim ktp = From btn As Button In Me.Controls.OfType(Of Button)() _
Where btn.Text.ToLower = Char.ToLower(e.KeyChar) _
Select btn
If ktp.Count > 0 Then ktp.First.PerformClick()
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 25th, 2010, 04:47 AM
#3
Frenzied Member
Re: calculator
Does it use the expression evaluation code from the CodeBank? You know, the one with the code compilation at runtime...
Please rate helpful ppl's posts. It's the best 'thank you' you can give
-
Jul 25th, 2010, 05:24 AM
#4
Re: calculator
no it doesn't. it's just a simple calculator
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 25th, 2010, 02:13 PM
#5
Member
Re: calculator
Does the download contain the source code? My calculator can only work with two textboxes, but I want one.
-
Jul 26th, 2010, 07:03 AM
#6
Re: calculator
Originally Posted by iProRyan
Does the download contain the source code?
yes it contains the source code
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 26th, 2010, 07:10 AM
#7
Re: calculator
heres's a bug i found.
it allows numbers to be appended to the total + can be fixed by adding a space after the "=" in the endswith:
Code:
Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click
If Not Label1.Text.EndsWith("=") Then TextBox1.Text &= DirectCast(sender, Button).Text
End Sub
amended code:
Code:
Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click
If Not Label1.Text.EndsWith("= ") Then TextBox1.Text &= DirectCast(sender, Button).Text
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 29th, 2010, 11:59 PM
#8
Lively Member
Re: calculator
wouldn't this be better for the btns_click event?
vb Code:
Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click If Not Label1.Text.EndsWith("= ") Then TextBox1.Text &= DirectCast(sender, Button).Text Else Label1.Text = "" TextBox1.Text = "" TextBox1.Text &= DirectCast(sender, Button).Text End If End Sub
-
Jul 30th, 2010, 08:21 AM
#9
Re: calculator
Originally Posted by reconrey
wouldn't this be better for the btns_click event?
the way it is at the moment, you can continue your calculations using the total as the first number. i want to keep it that way.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 30th, 2010, 08:51 AM
#10
Re: calculator
Originally Posted by .paul.
the way it is at the moment, you can continue your calculations using the total as the first number. i want to keep it that way.
ok i thought about it + that would make sense. i made a few other modifications too...
vb Code:
Public Class Form1
Private lastOperator As String
Private firstPart As Double
Private previousLastOperator As String
Private previousLastPart As Double
Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click
If Label1.Text.EndsWith("= ") Then
btnClear.PerformClick()
End If
TextBox1.Text &= DirectCast(sender, Button).Text
End Sub
Private Sub btnOperator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnOperator1.Click, btnOperator2.Click, btnOperator3.Click, btnOperator4.Click
Dim nextPart As Double
Double.TryParse(TextBox1.Text, nextPart)
If nextPart = 0 Then
If DirectCast(sender, Button).Text = "-" AndAlso TextBox1.Text = "" Then
TextBox1.Text &= "-"
End If
Return
End If
previousLastOperator = lastOperator
previousLastPart = nextPart
methods.calculate(TextBox1, firstPart, nextPart, lastOperator)
If lastOperator <> "" Then
Label1.Text &= nextPart.ToString
Else
Label1.Text &= firstPart.ToString
End If
lastOperator = DirectCast(sender, Button).Text
Label1.Text &= " " & lastOperator & " "
TextBox1.Text = ""
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim lastPart As Double
Double.TryParse(TextBox1.Text, lastPart)
If lastPart <> 0 Then
methods.calculate(TextBox1.Text, firstPart, lastPart, lastOperator)
Label1.Text &= " " & lastPart.ToString & " = "
Else
methods.changeText(Label1.Text, " = ")
TextBox1.Text = firstPart.ToString
End If
firstPart = 0
lastOperator = ""
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
TextBox1.Text = ""
firstPart = 0
lastOperator = ""
Label1.Text = ""
previousLastOperator = ""
previousLastPart = 0
End Sub
Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click
Dim nextPart As Double
Double.TryParse(TextBox1.Text, nextPart)
If nextPart <> 0 Then
methods.calculate(TextBox1, firstPart, nextPart, lastOperator)
If lastOperator <> "" Then
Label1.Text &= " " & nextPart.ToString & " √ = "
Else
Label1.Text &= firstPart.ToString & " √ = "
End If
End If
If nextPart = 0 AndAlso firstPart <> 0 Then
methods.changeText(Label1.Text, " √ = ")
End If
TextBox1.Text = Math.Sqrt(firstPart).ToString
Double.TryParse(TextBox1.Text, firstPart)
lastOperator = ""
End Sub
Private Sub btnPercentage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercentage.Click
Dim lastPart As Double
If Double.TryParse(TextBox1.Text, lastPart) AndAlso lastPart <> 0 Then
methods.calculate(TextBox1.Text, firstPart, lastPart, lastOperator, True)
If lastOperator <> "" Then
Label1.Text &= " " & lastPart.ToString & " % = "
End If
Double.TryParse(TextBox1.Text, firstPart)
lastOperator = ""
ElseIf lastPart = 0 AndAlso previousLastOperator <> "" Then
methods.calculate(firstPart, previousLastPart, previousLastOperator)
lastPart = previousLastPart
methods.calculate(TextBox1.Text, firstPart, lastPart, previousLastOperator, True)
methods.changeText(Label1.Text, " % = ")
Double.TryParse(TextBox1.Text, firstPart)
lastOperator = ""
previousLastOperator = ""
previousLastPart = 0
End If
End Sub
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Dim ktp = From btn As Button In Me.Controls.OfType(Of Button)() _
Where btn.Text.ToLower = Char.ToLower(e.KeyChar) _
Select btn
If ktp.Count > 0 Then ktp.First.PerformClick()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim r As New Rectangle(TextBox1.Left - 1, Label1.Top - 1, Label1.Width + 2, Label1.Height + TextBox1.Height + 2)
ControlPaint.DrawBorder3D(e.Graphics, r, Border3DStyle.SunkenInner)
End Sub
End Class
Last edited by .paul.; Aug 6th, 2010 at 09:40 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|