Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'creator : ibennz
- 'TODO : handle multiples operations with multi levels array , handle overflows and some errors
- Option Strict On
- Public Class Form1
- Sub New()
- ' This call is required by the designer.
- InitializeComponent()
- AddHandler MouseDown, AddressOf MouseDownHandling
- MouseDownSetting(Me)
- ' Add any initialization after the InitializeComponent() call.
- End Sub
- Private Sub MouseDownSetting(ByVal parentCtr As Control)
- Dim ctr As Control
- For Each ctr In parentCtr.Controls
- If TypeOf ctr Is Button Then
- AddHandler ctr.MouseDown, AddressOf MouseDownHandling
- MouseDownSetting(ctr)
- End If
- Next
- End Sub
- 'cleaner than fkn with each buttons
- Private IsDoingOtherTask As Integer = 0
- Private OldInput As Double = 0
- Private Answer As Double = 0
- Private Sub MouseDownHandling(ByVal sender As Object, ByVal e As MouseEventArgs)
- 'direcast the sender object with his original type (control)
- Dim MainNumbers As String = DirectCast(sender, Control).Text
- Select Case IsDoingOtherTask
- Case Tasks.Additioning
- MainNumbers = MainNumbers.Insert(0, "+")
- Case Tasks.finishing
- MainNumbers = MainNumbers.Insert(0, "f")
- Case Tasks.Substracting
- MainNumbers = MainNumbers.Insert(0, "-")
- Case Tasks.Multiplying
- MainNumbers = MainNumbers.Insert(0, "x")
- Case Tasks.Dividing
- MainNumbers = MainNumbers.Insert(0, "/")
- End Select
- If MainNumbers.Contains("c") Then
- MainNumbers = "other"
- End If
- Select Case MainNumbers
- Case "-"
- IsDoingOtherTask = Tasks.Substracting
- OldInput = CDbl(TextBox1.Text)
- Case "+"
- IsDoingOtherTask = Tasks.Additioning
- OldInput = CDbl(TextBox1.Text)
- Case "*"
- IsDoingOtherTask = Tasks.Multiplying
- OldInput = CDbl(TextBox1.Text)
- Case "/"
- IsDoingOtherTask = Tasks.Dividing
- OldInput = CDbl(TextBox1.Text)
- Case "0"
- If TextBox1.Text = "0" Then
- TextBox1.Text = "0"
- Exit Select
- End If
- TextBox1.Text &= "0"
- OldInput = 0
- Case "1"
- TextBox1.Text &= "1"
- OldInput = 1
- Case "2"
- TextBox1.Text &= "2"
- OldInput = 2
- Case "3"
- TextBox1.Text &= "3"
- OldInput = 3
- Case "4"
- TextBox1.Text &= "4"
- OldInput = 4
- Case "5"
- TextBox1.Text &= "5"
- OldInput = 5
- Case "6"
- TextBox1.Text &= "6"
- OldInput = 6
- Case "7"
- TextBox1.Text &= "7"
- OldInput = 7
- Case "8"
- TextBox1.Text &= "8"
- OldInput = 8
- Case "9"
- TextBox1.Text &= "9"
- OldInput = 9
- Case ","
- If TextBox1.Text.Contains(",") Then Exit Select
- If TextBox1.Text.Length >= 1 Then
- OldInput = CDbl(TextBox1.Text)
- TextBox1.Text &= ","
- End If
- Case "+" & DirectCast(sender, Control).Text
- If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
- Exit Select
- End If
- TextBox1.Text = DirectCast(sender, Control).Text
- Answer = CDbl(DirectCast(sender, Control).Text) + OldInput
- OldInput = 0
- IsDoingOtherTask = Tasks.finishing
- Case "-" & DirectCast(sender, Control).Text
- If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
- Exit Select
- End If
- TextBox1.Text = DirectCast(sender, Control).Text
- Answer = OldInput - CDbl(DirectCast(sender, Control).Text)
- OldInput = 0
- IsDoingOtherTask = Tasks.finishing
- Case "/" & DirectCast(sender, Control).Text
- If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
- Exit Select
- End If
- TextBox1.Text = DirectCast(sender, Control).Text
- Answer = OldInput / CDbl(DirectCast(sender, Control).Text)
- OldInput = 0
- IsDoingOtherTask = Tasks.finishing
- Case "x" & DirectCast(sender, Control).Text
- If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
- Exit Select
- End If
- TextBox1.Text = DirectCast(sender, Control).Text
- Answer = OldInput * CDbl(DirectCast(sender, Control).Text)
- OldInput = 0
- IsDoingOtherTask = Tasks.finishing
- Case "f" & DirectCast(sender, Control).Text
- If "f" & DirectCast(sender, Control).Text = "f=" Then
- TextBox1.Text = CStr(Answer)
- IsDoingOtherTask = Tasks.finishing
- Exit Sub
- End If
- If Not IsNumeric(DirectCast(sender, Control).Text) Then
- IsDoingOtherTask = Tasks.idle
- Exit Select
- End If
- TextBox1.Text = DirectCast(sender, Control).Text
- IsDoingOtherTask = Tasks.idle
- Case Else
- 'magestically handle the errors
- TextBox1.Text = "0"
- OldInput = 0
- Answer = 0
- IsDoingOtherTask = Tasks.idle
- End Select
- 'carret scroll
- If TextBox1.Text(0) = "0" And TextBox1.Text.Length > 1 And Not (TextBox1.Text.Contains(",")) Then
- TextBox1.Text = TextBox1.Text.Remove(0, 1)
- End If
- TextBox1.Select(0, TextBox1.Text.Length)
- End Sub
- Private Structure Tasks
- Const idle As Integer = 0
- Const Additioning As Integer = 1
- Const finishing As Integer = 2
- Const Substracting As Integer = 3
- Const Multiplying As Integer = 4
- Const Dividing As Integer = 5
- End Structure
- End Class
Advertisement
Add Comment
Please, Sign In to add comment