Question

In: Computer Science

Using C# visual basics Can someone show a calculator with a button that does a While...

Using C# visual basics

Can someone show a calculator with a button that does a While LOOP about 10x time and does a print out of a name 10x

Solutions

Expert Solution

Public Class calculator

    ' two numbers to do the calc

    Dim num1, num2 As Double

     ' check if an operator is clicked for the first time

    Dim oprClickCount As Integer = 0

     ' check if an operator is clicked befor

    Dim isOprClick As Boolean = False

     ' check if equal is clicked befor

    Dim isEqualClick As Boolean = False

     ' get the operator

    Dim opr As String

    Private Sub calculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' add click event to all button in the form

        For Each c As Control In Controls

            ' if the control is button

            If c.GetType() = GetType(Button) Then

                If Not c.Text.Equals("Reset") Then

                   ' add action to the button

                    AddHandler c.Click, AddressOf btn_Click

                End If

               

            End If

        Next

    End Sub

    ' create a button click event

    Private Sub btn_Click(sender As Object, e As EventArgs)

        Dim button As Button = sender

        If Not isOperator(button) Then

            ' if number

            If isOprClick Then

                ' if an opr is clicked

                ' get and convert to double textbox text

                num1 = Double.Parse(TextBox1.Text)

                ' clear textbox text

                TextBox1.Text = ""

            End If

            If Not TextBox1.Text.Contains(".") Then

                ' if "." not already in the textbox

                If TextBox1.Text.Equals("0") AndAlso Not button.Text.Equals(".") Then

                    TextBox1.Text = button.Text

                    isOprClick = False

                Else

                    TextBox1.Text += button.Text

                    isOprClick = False

                End If

            ElseIf Not button.Text.Equals(".") Then

                ' if the button is not a "."

                TextBox1.Text += button.Text

                isOprClick = False

            End If

        Else

          ' if operator

            If oprClickCount = 0 Then

                ' if we click on an operator for the first time

                oprClickCount += 1

                num1 = Double.Parse(TextBox1.Text)

                opr = button.Text

                isOprClick = True

            Else

                If Not button.Text.Equals("=") Then

                    ' if the button is not "="

                    If Not isEqualClick Then

                        ' if "=" is not clicked befor

                        num2 = Double.Parse(TextBox1.Text)

                        TextBox1.Text = Convert.ToString(calc(opr, num1, num2))

                        num2 = Double.Parse(TextBox1.Text)

                        opr = button.Text

                        isOprClick = True

                        isEqualClick = False

                    Else

                        isEqualClick = False

                        opr = button.Text

                    End If

                Else

                    num2 = Double.Parse(TextBox1.Text)

                    TextBox1.Text = Convert.ToString(calc(opr, num1, num2))

                    num1 = Double.Parse(TextBox1.Text)                   

                    isOprClick = True

                    isEqualClick = True

                End If

            End If

        End If

    End Sub

    ' create a function to check if the button is a number or an operator

    Function isOperator(ByVal btn As Button) As Boolean

        Dim btnText As String

        btnText = btn.Text

        If (btnText.Equals("+") Or btnText.Equals("-") Or btnText.Equals("/") Or

            btnText.Equals("X") Or btnText.Equals("=")) Then

            Return True

        Else

            Return False

        End If

    End Function

    ' create a function to do the calc

    Function calc(ByVal op As String, ByVal n1 As Double, ByVal n2 As Double) As Double

                        

        Dim result As Double

        result = 0

        Select Case op

            Case "+"

                result = n1 + n2

            Case "-"

                result = n1 - n2

            Case "X"

                result = n1 * n2

            Case "/"

                If n2 <> 0 Then

                    result = n1 / n2

                End If

        End Select

        Return result

    End Function

    Private Sub ButtonReset_Click(sender As Object, e As EventArgs) Handles ButtonReset.Clic

        num1 = 0

        num2 = 0

        opr = ""

        oprClickCount = 0

        isOprClick = False

        isEqualClick = False

        TextBox1.Text = "0"

    End Sub

End Class


Related Solutions

Using C# visual basics Can someone show a calculator with a button that does a FOR...
Using C# visual basics Can someone show a calculator with a button that does a FOR LOOP about 10x time and does a print out of a name 10x
How can I configure the button in this tip calculator to calculate the total using the...
How can I configure the button in this tip calculator to calculate the total using the entires for the bill and tip percentage? I'm using Visual Studio 2019 Xamarin.Forms Main.Page.xaml <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Accomplish_2.MainPage" BackgroundColor="Gray" Padding="5"> <StackLayout> <!-- Place new controls here --> <Label Text="Tip Calculator" HorizontalOptions="Center" VerticalOptions="Center" FontSize="Title" FontAttributes="Bold"/> <BoxView BackgroundColor="LightPink" HeightRequest="3"></BoxView>    <Entry Placeholder="Bill Total" Keyboard="Numeric" x:Name="billTotal"></Entry>    <Entry Placeholder="Tip Percentage" Keyboard="Numeric" x:Name="tipPercent"></Entry> <Button Text="Calculate" Clicked="Button_Clicked"></Button>    </StackLayout> </ContentPage> Everything above...
Add a radio Button in UI , by using Visual Studio code Add a Radio button...
Add a radio Button in UI , by using Visual Studio code Add a Radio button in UI , by Using visual Studio code in Angular or Angular Js Format.
Need it in C# using visual studio (also can you show me how to implement the...
Need it in C# using visual studio (also can you show me how to implement the code in vs) Create a New Project named Preferred Customer to be used in a retail store for preferred customers, where customers can earn discount on all purchases depending upon how much money they are going to spend. To begin with designing your Application, you must add a class named Person with properties for holding a Person’s Name, Address and Telephone Number. Next step...
Can someone show me how to make this javaFx code work? The submit button should remain...
Can someone show me how to make this javaFx code work? The submit button should remain disabled until: ● There is text in all three fields. ● The two password fields have the same value. When Submit is clicked, display an Alert that says “Account Created!” When Quit is clicked, display an Alert that asks the user if they are sure they want to quit. If they click OK, quit the program with System.exit(0). If they click Cancel, the program...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store data...
Write a program in Visual Basics that does the following: Read in 20 numbers, each of...
Write a program in Visual Basics that does the following: Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.
Create an ASP.Net Website using Visual Studio with Visual Basic.Net: Create a simple calculator that has...
Create an ASP.Net Website using Visual Studio with Visual Basic.Net: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store...
can someone translate this pseudo code to actual c++ code while (not the end of the...
can someone translate this pseudo code to actual c++ code while (not the end of the input) If the next input is a number read it and push it on the stack else If the next input is an operator, read it pop 2 operands off of the stack apply the operator push the result onto the stack When you reach the end of the input: if there is one number on the stack, print it else error
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code for calculator menus radio button input text boxes
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT