﻿
Public Class Form1
    Public op As String
    Public op1 As Double
    Public op2 As Double
    Public check_op As Boolean

    Private Sub Zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Zero.Click, One.Click, Two.Click, Three.Click, Four.Click, Five.Click, Six.Click, Seven.Click, Eight.Click, Nine.Click
        If check_op = True Then
            TextBox1.Clear()
        End If
        If TextBox1.Text = "0" Then
            TextBox1.Clear()
            TextBox1.Text = TextBox1.Text + sender.Text
        Else
            TextBox1.Text = TextBox1.Text + sender.Text
        End If
        check_op = False

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "0"
    End Sub

    Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click, BackClear.Click
        Select Case sender.Name
            Case "clear"
                TextBox1.Clear()
                TextBox1.Text = "0"
                op1 = 0
                op2 = 0
                check_op = False
            Case "BackClear"
                Dim i As Integer = Len(TextBox1.Text)
                If i = 1 Then
                    TextBox1.Text = "0"
                Else
                    TextBox1.Text = RSet(TextBox1.Text, i - 1)
                End If
        End Select
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decimalpoint.Click
        If InStr(TextBox1.Text, ".") > 0 Then
            ' nothing to do
        ElseIf TextBox1.Text = "0" Then
            TextBox1.Clear()
            TextBox1.Text = "0." + TextBox1.Text
        Else
            TextBox1.Text = TextBox1.Text + "."
        End If
    End Sub

    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
        If Val(TextBox1.Text) < 0 Then
            MsgBox("لايمكن ايجاد الجذر لعدد سالب", MsgBoxStyle.Critical, "خطاء")
            Exit Sub
        End If
        TextBox1.Text = Math.Sqrt(Val(TextBox1.Text))
        check_op = True
    End Sub

    Private Sub square_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles square.Click, overThree.Click, mode.Click, overN.Click
        Select Case sender.Name
            Case "square"
                TextBox1.Text = Math.Pow(Val(TextBox1.Text), 2)
            Case "overThree"
                TextBox1.Text = Math.Pow(Val(TextBox1.Text), 3)
            Case "mode"
                op1 = Val(TextBox1.Text)
                TextBox1.Text = op1 & " mod " & "(n)"
                op = "mode"
            Case "overN"
                op1 = Val(TextBox1.Text)
                TextBox1.Text = op1 & " ^ " & "(n)"
                op = "overN"
        End Select
        check_op = True
    End Sub

    Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click, subt.Click, mult.Click, div.Click
        Select Case sender.Name
            Case "add"
                op1 = Val(TextBox1.Text)
                op = "add"
            Case "subt"
                op1 = Val(TextBox1.Text)
                op = "subt"
            Case "div"
                op1 = Val(TextBox1.Text)
                op = "div"
            Case "mult"
                op1 = Val(TextBox1.Text)
                op = "mult"
        End Select
        check_op = True
    End Sub

    Private Sub equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equal.Click
        Select Case op
            Case "add"
                op2 = Val(TextBox1.Text)
                TextBox1.Text = op1 + op2
            Case "subt"
                op2 = Val(TextBox1.Text)
                TextBox1.Text = op1 - op2
            Case "div"
                op2 = Val(TextBox1.Text)
                If op2 = 0 Then
                    MsgBox("لايمكن القسمة على صفر", MsgBoxStyle.Critical, "خطاء")
                    Exit Sub
                End If
                TextBox1.Text = op1 / op2
            Case "mult"
                op2 = Val(TextBox1.Text)
                TextBox1.Text = op1 * op2
            Case "mode"
                op2 = Val(TextBox1.Text)
                TextBox1.Text = op1 Mod op2
            Case "overN"
                op2 = Val(TextBox1.Text)
                TextBox1.Text = op1 ^ op2
        End Select
        check_op = True
    End Sub

    Private Sub نسخToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles نسخToolStripMenuItem.Click
        Clipboard.Clear()
        Clipboard.SetText(TextBox1.Text)
    End Sub

    Private Sub لصقToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles لصقToolStripMenuItem.Click
        TextBox1.Clear()
        TextBox1.Text = Clipboard.GetText()
    End Sub

    Private Sub PlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusMinus.Click
        TextBox1.Text = -1 * Val(TextBox1.Text)
    End Sub
End Class

