NelloRizzo

[VBNET] Simple Calc Windows forms

Feb 15th, 2017
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.75 KB | None | 0 0
  1. Public Class MainForm
  2.     ' Facilitazioni per accedere ai valori inseriti nel form
  3.     ' accede alla casella di testo del primo operando
  4.     Property First As Single
  5.         Get
  6.             Dim r As Single = txtFirst.Text
  7.             Return r
  8.         End Get
  9.         Set(value As Single)
  10.             txtFirst.Text = value
  11.         End Set
  12.     End Property
  13.     ' accede alla casella di testo del secondo operando
  14.     Property Second As Single
  15.         Get
  16.             Dim r As Single = CSng(txtSecond.Text)
  17.             Return r
  18.         End Get
  19.         Set(value As Single)
  20.             txtSecond.Text = value
  21.         End Set
  22.     End Property
  23.     ' accede alla combo box delle operazioni
  24.     Property Operation As String
  25.         Get
  26.             Return cbOperation.SelectedItem
  27.         End Get
  28.         Set(value As String)
  29.             cbOperation.SelectedItem = "+"
  30.         End Set
  31.     End Property
  32.     ' costruttore
  33.     Public Sub New()
  34.  
  35.         ' La chiamata è richiesta dalla finestra di progettazione.
  36.         InitializeComponent()
  37.  
  38.         ' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
  39.  
  40.         ' inizializza il form
  41.         First = 0
  42.         Second = 0
  43.         Operation = "+"
  44.         ' PORTA IL MOUSE SUL PRIMO OPERANDO
  45.         txtFirst.Focus()
  46.     End Sub
  47.     ' Intercetta i caratteri inseriti nelle caselle di testo
  48.     ' e accetta solo cifre e una sola occorrenza della virgola
  49.     Private Sub AcceptOnlyNumbers(sender As Object, e As KeyPressEventArgs) Handles txtSecond.KeyPress, txtFirst.KeyPress
  50.         ' controlla se il carattere digitato è un carattere di
  51.         ' controllo (frecce, cancellazione ecc)
  52.         If Not Char.IsControl(e.KeyChar) Then
  53.             Dim t As TextBox = sender ' accede alla casella di testo
  54.             Dim text As String = t.Text ' recupera il testo inserito
  55.             ' recupera la virgola decimale in uso nel sistema
  56.             Dim comma As String = Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator
  57.             'controlla se è già presente una virgola decimale
  58.             Dim acceptDot = text.IndexOf(comma) = -1
  59.             ' se l'utente ha premuto sul punto
  60.             ' o sull'eventuale simbolo della virgola usato
  61.             If e.KeyChar = "." OrElse e.KeyChar = comma Then
  62.                 If Not acceptDot Then ' se non è accettabile
  63.                     ' SCARTA L'EVENTO
  64.                     ' comunicando che non si deve fare più nulla
  65.                     ' perché l'evento è da considerarsi GESTITO
  66.                     e.Handled = True
  67.                 Else
  68.                     ' altrimenti mette la virgola
  69.                     e.KeyChar = comma
  70.                 End If
  71.                 ' qui controlla che si tratti invece di una cifra
  72.             ElseIf Not Char.IsDigit(e.KeyChar) Then
  73.                 e.Handled = True
  74.             End If
  75.         End If
  76.     End Sub
  77.     ' seleziona il contenuto della casella di testo quando l'utente entra in essa
  78.     ' con il tasto tabulazione o con il mouse
  79.     Private Sub UserEnterInBox(sender As Object, e As EventArgs) Handles txtSecond.Enter, txtFirst.Enter, txtSecond.Click, txtFirst.Click
  80.         Dim t As TextBox = sender
  81.         t.SelectAll()
  82.     End Sub
  83.     ' effettua il calcolo e produce il risultato
  84.     Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
  85.         Dim result As Single = First
  86.         Select Case Operation
  87.             Case "+"
  88.                 result += Second
  89.             Case "-"
  90.                 result -= Second
  91.             Case "*"
  92.                 result *= Second
  93.             Case "/"
  94.                 result /= Second
  95.         End Select
  96.         lblResult.Text = result
  97.     End Sub
  98. End Class
Add Comment
Please, Sign In to add comment