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...
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
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters the course she is taking in the first text box and then clicks on the button. Then if the user entered "Program Language" in the first text box, the second text box will show "Awesome pick!". If the user entered something else, the text box shows "Ok." Please write the correct code.
Please use original C++ code for this. This is for visual studio. Program 5: Shipping Calculator...
Please use original C++ code for this. This is for visual studio. Program 5: Shipping Calculator The Speedy Shipping Company will ship packages based on how much they weigh and how far they are being sent. They will only ship small packages up to 10 pounds. You have been tasked with writing a program that will help Speedy Shipping determine how much to charge per delivery. The charges are based on each 500 miles shipped. Shipping charges are not pro-rated;...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication,...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should request a numerical input from the user, followed by the operation to be performed, and the second number to complete the equation. The result should be displayed to the user after each equation is performed. For example, if the user performs 3+3, the program should display 6 as the result. The program should continue running so the user can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT