Oppure

Loading
27/09/14 15:25
WCodeLyokoYT
Sto creando una calcolatrice in VB.Net e come da titolo non riesco ad inserire il punto(o la virgola) nelle variabili Valore1 e Valore2.

Allego il codice:

Public Class Form1
    Dim Valore1 As Double
    Dim Valore2 As Double
    Dim Totale As Double
    Dim Op As String
    Dim Val2 As Boolean
#Region "Numeri"
    Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "1"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "1"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "2"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "2"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "3"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "3"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "4"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "4"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "5"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "5"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "6"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "6"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "7"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "7"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "8"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "8"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "9"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "9"
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & ","
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & ","
            RichTextBox1.Text = Valore2
        End If
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        If Val2 = 0 Then
            Valore1 = Valore1 & "0"
            RichTextBox1.Text = Valore1
        Else
            Valore2 = Valore2 & "0"
            RichTextBox1.Text = Valore2
        End If
    End Sub

#End Region

#Region "Operazioni"
    Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click

        Op = "+"
        Val2 = 1
        RichTextBox1.Text = "0"

    End Sub

    Private Sub Button27_Click(sender As Object, e As EventArgs) Handles Button27.Click
        Op = "-"
        Val2 = 1
        RichTextBox1.Text = "0"
    End Sub

    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        Op = "*"
        Val2 = 1
        RichTextBox1.Text = "0"
    End Sub

    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        Op = "/"
        Val2 = 1
        RichTextBox1.Text = "0"
    End Sub
#End Region


    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        If Op <> "" Then
            If Op = "+" Then
                Totale = Valore1 + Valore2
            End If
            If Op = "-" Then
                Totale = Valore1 - Valore2
            End If
            If Op = "*" Then
                Totale = Valore1 * Valore2
            End If
            If Op = "/" Then
                Totale = Valore1 / Valore2
            End If
            RichTextBox1.Text = Totale
            Valore1 = "0"
            Valore2 = "0"
            Val2 = 0
            Op = 0
        End If
    End Sub
End Class
aaa
27/09/14 16:29
TheDarkJuster
in che senso non riesci a "mettere" il punto o la virgola? Dove hai trovato quel codice? Lo hai modificato? Se fai la stessa cosa col punto di ciò che hai fatto con i numeri non viene fuori esattamente ciò che vuoi? Non riesco a capire il problema, per questo di chiedo se il codice è copiato.
aaa
27/09/14 19:16
Poggi Marco
Ciao!

Il tuo errore sta nella conversione implicita Valore2 = Valore2 & "," nella funzione Button11_Click.
Dopo l'operazione, Valore2 rimane invariato, e il comando della virgola non viene registrato.

Ti consiglio di cambiare approccio al problema; ad esempio immetti i dati in input su una stringa.
aaa
27/09/14 20:21
WCodeLyokoYT
Allora intanto grazie per le risposte. Poi il codice l'ho creato io e il problema è nella funzione Button11_click come ha detto Marco.
Semplicemente non mette la virgola nella variabile, perché vado a riscrivere la variabile nella textbox essa non appare.
Ho provato a dichiarare le variabili come stringhe, ma non riesco a fare l'operazione matematica di cui ho bisogno.
Sapreste aiutarmi?

Grazie.
aaa
27/09/14 20:51
WCodeLyokoYT
Ok credo di aver trovato una possibile soluzione, anche se molto complessa...
aaa
28/09/14 15:30
dnha
Ciao :)
posta la soluzione che potrebbe essere utile ad altri :k:
aaa
28/09/14 17:29
TheDarkJuster
Ma non puoi semplicemente comporre un numero con numero = numero + x * 10^posizione? X è il numero del tasto, vanno da 0 a 9 e ogni volta che lo fai aggiungi uno alla posizione. Quando premi la virgola posizione diventa - 1 e ogni volta che applichi quella formula sottrai uno a posizione. Se hai necessità di stampare il numero puoi usare il metodo toString
Ultima modifica effettuata da TheDarkJuster 28/09/14 17:31
aaa
28/09/14 17:54
dnha
Avevo pensato anche io a questa soluzione
aaa