ibennz

Basic calc

Jul 30th, 2014
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.50 KB | None | 0 0
  1. 'creator : ibennz
  2. 'TODO : handle multiples operations with multi levels array , handle overflows and some errors
  3. Option Strict On
  4. Public Class Form1
  5.     Sub New()
  6.  
  7.         ' This call is required by the designer.
  8.         InitializeComponent()
  9.         AddHandler MouseDown, AddressOf MouseDownHandling
  10.         MouseDownSetting(Me)
  11.         ' Add any initialization after the InitializeComponent() call.
  12.  
  13.     End Sub
  14.     Private Sub MouseDownSetting(ByVal parentCtr As Control)
  15.         Dim ctr As Control
  16.         For Each ctr In parentCtr.Controls
  17.             If TypeOf ctr Is Button Then
  18.                 AddHandler ctr.MouseDown, AddressOf MouseDownHandling
  19.                 MouseDownSetting(ctr)
  20.  
  21.             End If
  22.         Next
  23.     End Sub
  24.     'cleaner than fkn with each buttons
  25.     Private IsDoingOtherTask As Integer = 0
  26.     Private OldInput As Double = 0
  27.     Private Answer As Double = 0
  28.     Private Sub MouseDownHandling(ByVal sender As Object, ByVal e As MouseEventArgs)
  29.         'direcast the sender object with his original type (control)
  30.         Dim MainNumbers As String = DirectCast(sender, Control).Text
  31.        
  32.  
  33.         Select Case IsDoingOtherTask
  34.             Case Tasks.Additioning
  35.                 MainNumbers = MainNumbers.Insert(0, "+")
  36.             Case Tasks.finishing
  37.                 MainNumbers = MainNumbers.Insert(0, "f")
  38.             Case Tasks.Substracting
  39.                 MainNumbers = MainNumbers.Insert(0, "-")
  40.             Case Tasks.Multiplying
  41.                 MainNumbers = MainNumbers.Insert(0, "x")
  42.             Case Tasks.Dividing
  43.                 MainNumbers = MainNumbers.Insert(0, "/")
  44.         End Select
  45.  
  46.         If MainNumbers.Contains("c") Then
  47.             MainNumbers = "other"
  48.         End If
  49.      
  50.         Select Case MainNumbers
  51.             Case "-"
  52.                 IsDoingOtherTask = Tasks.Substracting
  53.                 OldInput = CDbl(TextBox1.Text)
  54.             Case "+"
  55.                 IsDoingOtherTask = Tasks.Additioning
  56.                 OldInput = CDbl(TextBox1.Text)
  57.  
  58.             Case "*"
  59.                 IsDoingOtherTask = Tasks.Multiplying
  60.                 OldInput = CDbl(TextBox1.Text)
  61.  
  62.             Case "/"
  63.                 IsDoingOtherTask = Tasks.Dividing
  64.                 OldInput = CDbl(TextBox1.Text)
  65.  
  66.             Case "0"
  67.                 If TextBox1.Text = "0" Then
  68.                     TextBox1.Text = "0"
  69.                     Exit Select
  70.                 End If
  71.                 TextBox1.Text &= "0"
  72.                 OldInput = 0
  73.             Case "1"
  74.                 TextBox1.Text &= "1"
  75.                 OldInput = 1
  76.             Case "2"
  77.                 TextBox1.Text &= "2"
  78.                 OldInput = 2
  79.             Case "3"
  80.                 TextBox1.Text &= "3"
  81.                 OldInput = 3
  82.             Case "4"
  83.                 TextBox1.Text &= "4"
  84.                 OldInput = 4
  85.             Case "5"
  86.                 TextBox1.Text &= "5"
  87.                 OldInput = 5
  88.             Case "6"
  89.                 TextBox1.Text &= "6"
  90.                 OldInput = 6
  91.             Case "7"
  92.                 TextBox1.Text &= "7"
  93.                 OldInput = 7
  94.             Case "8"
  95.                 TextBox1.Text &= "8"
  96.                 OldInput = 8
  97.             Case "9"
  98.                 TextBox1.Text &= "9"
  99.                 OldInput = 9
  100.             Case ","
  101.                 If TextBox1.Text.Contains(",") Then Exit Select
  102.                 If TextBox1.Text.Length >= 1 Then
  103.                     OldInput = CDbl(TextBox1.Text)
  104.                     TextBox1.Text &= ","
  105.                 End If
  106.             Case "+" & DirectCast(sender, Control).Text
  107.                 If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
  108.                     Exit Select
  109.                 End If
  110.                 TextBox1.Text = DirectCast(sender, Control).Text
  111.                 Answer = CDbl(DirectCast(sender, Control).Text) + OldInput
  112.                 OldInput = 0
  113.                 IsDoingOtherTask = Tasks.finishing
  114.             Case "-" & DirectCast(sender, Control).Text
  115.                 If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
  116.                     Exit Select
  117.                 End If
  118.                 TextBox1.Text = DirectCast(sender, Control).Text
  119.                 Answer = OldInput - CDbl(DirectCast(sender, Control).Text)
  120.                 OldInput = 0
  121.                 IsDoingOtherTask = Tasks.finishing
  122.             Case "/" & DirectCast(sender, Control).Text
  123.                 If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
  124.                     Exit Select
  125.                 End If
  126.                 TextBox1.Text = DirectCast(sender, Control).Text
  127.                 Answer = OldInput / CDbl(DirectCast(sender, Control).Text)
  128.                 OldInput = 0
  129.                 IsDoingOtherTask = Tasks.finishing
  130.             Case "x" & DirectCast(sender, Control).Text
  131.                 If Not (IsNumeric(DirectCast(sender, Control).Text)) Then
  132.                     Exit Select
  133.                 End If
  134.                 TextBox1.Text = DirectCast(sender, Control).Text
  135.                 Answer = OldInput * CDbl(DirectCast(sender, Control).Text)
  136.                 OldInput = 0
  137.                 IsDoingOtherTask = Tasks.finishing
  138.             Case "f" & DirectCast(sender, Control).Text
  139.                 If "f" & DirectCast(sender, Control).Text = "f=" Then
  140.                     TextBox1.Text = CStr(Answer)
  141.                     IsDoingOtherTask = Tasks.finishing
  142.                     Exit Sub
  143.                 End If
  144.                 If Not IsNumeric(DirectCast(sender, Control).Text) Then
  145.                     IsDoingOtherTask = Tasks.idle
  146.                     Exit Select
  147.                 End If
  148.                 TextBox1.Text = DirectCast(sender, Control).Text
  149.                 IsDoingOtherTask = Tasks.idle
  150.  
  151.             Case Else
  152.                 'magestically handle the errors
  153.                 TextBox1.Text = "0"
  154.                 OldInput = 0
  155.                 Answer = 0
  156.                 IsDoingOtherTask = Tasks.idle
  157.         End Select
  158.  
  159.         'carret scroll
  160.         If TextBox1.Text(0) = "0" And TextBox1.Text.Length > 1 And Not (TextBox1.Text.Contains(",")) Then
  161.             TextBox1.Text = TextBox1.Text.Remove(0, 1)
  162.         End If
  163.         TextBox1.Select(0, TextBox1.Text.Length)
  164.     End Sub
  165.     Private Structure Tasks
  166.         Const idle As Integer = 0
  167.         Const Additioning As Integer = 1
  168.         Const finishing As Integer = 2
  169.         Const Substracting As Integer = 3
  170.         Const Multiplying As Integer = 4
  171.         Const Dividing As Integer = 5
  172.     End Structure
  173. End Class
Advertisement
Add Comment
Please, Sign In to add comment