Jump to content

Visual Studio 2017 Calculator Help

Hello, below is an equals button method for a calculator that was made in Visual Studio 2017 that is in C#.

Can anyone help me to append it and get the history.text, put it into a string variable, so that I can add the string to a ListBox to store the history.


        protected void ButtonEquals_Click(object sender, EventArgs e)
        {
            if (calc_result.Value == string.Empty)
            {   // User must enter a value for the button to function correctly.
                Response.Write("<script>alert('No Value is given.')</script>");
            }
            else
            {
                ViewState["Value2"] = calc_result.Value;
                calc_result.Value = string.Empty;

                try
                {
                    if ((string)ViewState["Operation"] == "Addition")
                    {
                        calc_result.Value = _Calculate.Add(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    }
                    else if ((string)ViewState["Operation"] == "Subtraction")
                    {
                        calc_result.Value = _Calculate.Subtract(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    }
                    else if ((string)ViewState["Operation"] == "Multiplication")
                    {
                        calc_result.Value = _Calculate.Multiply(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    }
                    else if ((string)ViewState["Operation"] == "Division")
                    {
                        calc_result.Value = _Calculate.Divide(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    }
                    else if ((string)ViewState["Operation"] == "Percentage")
                    {
                        calc_result.Value = _Calculate.Percentage(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    }
                    else Response.Write("<script>alert('No Operation was recorded.')</script>");
                }
                catch (FormatException)
                {
                    Response.Write("<script>alert('Bad Input Format.')</script>");
                }
            }
        }

Link to comment
https://linustechtips.com/topic/899019-visual-studio-2017-calculator-help/
Share on other sites

Link to post
Share on other sites

create a variable to hold the history

string = historyText;

 

create a method and call it after every response (with the responseText being your string alerts):

        void historyTextFormatter(string responseText)
        {
            historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + responseText + Environment.NewLine;
        }

 

Link to post
Share on other sites

21 minutes ago, Rain7 said:

create a variable to hold the history


string = historyText;

 

create a method and call it after every response (with the responseText being your string alerts):


        void historyTextFormatter(string responseText)
        {
            historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + responseText + Environment.NewLine;
        }

 

so far this is what I put in

void HistoryTextFormatter(string responseText)
{
     string HistoryText = null;
     HistoryText = HistoryText = "[" + DateTime.Now.ToString() + "] " + responseText + Environment.NewLine;
}

 

I called the method but in the equals button click but it doesn't know what responseText is.

Link to post
Share on other sites

Just now, Sdot said:

so far this is what I put in

 

 



 

void HistoryTextFormatter(string responseText)

{

     string HistoryText = null;

     HistoryText = HistoryText = "[" + DateTime.Now.ToString() + "] " + responseText + Environment.NewLine;

}

[/code}

 

I called the method but in the equals button click but it doesn't know what responseText is.

If you declare the HistoryText inside the method then it'll get reset every time you use it, you should declare a global variable to store the HistoryText to retain the entirety of it. Declare it below the partial class.

 

The responseText will be provided by you, it shall contain the alert you wish to input for your history such as "No values is given", "Bad input format".

 

I'd personally store those inside a switch case along with the HistoryTextFormatter method.

 

You can do it like this:

        void HistoryTextFormatter(int responseCode)
        {
            string response;
            switch (responseCode)
            {
                case 1:
                    response = "No Value is given.";
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 2:
                    response = _Calculate.Add(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 3:
                    response = _Calculate.Subtract(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 4:
                    response = _Calculate.Multiply(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 5:
                    response = _Calculate.Divide(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 6:
                    response = _Calculate.Percentage(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 7:
                    response = "No Operation was recorded.";
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                case 8:
                    response = "Bad Input Format.";
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
                default:
                    response = "Unknown response, contact an admin immediately";
                    Response.Write("<script>alert('" + response + "')</script>");
                    historyText = historyText + "[" + DateTime.Now.ToString() + "]: " + response + Environment.NewLine;
                    break;
            }
        }

        protected void ButtonEquals_Click(object sender, EventArgs e)
        {
            // Switch Response Codes
            // 1 = No Value is Given
            // 2 = Addition
            // 3 = Subtraction
            // 4 = Multiplication
            // 5 = Division
            // 6 = Percentage
            // 7 = No Operation was recorded
            // 8 = Bad input format

            if (calc_result.Value == string.Empty)
            {   // User must enter a value for the button to function correctly.
                HistoryTextFormatter(1);
            }
            else
            {
                ViewState["Value2"] = calc_result.Value;
                calc_result.Value = string.Empty;

                try
                {
                    if ((string)ViewState["Operation"] == "Addition")
                    {
                        HistoryTextFormatter(2);
                    }
                    else if ((string)ViewState["Operation"] == "Subtraction")
                    {
                        HistoryTextFormatter(3);
                    }
                    else if ((string)ViewState["Operation"] == "Multiplication")
                    {
                        HistoryTextFormatter(4);
                    }
                    else if ((string)ViewState["Operation"] == "Division")
                    {
                        HistoryTextFormatter(5);
                    }
                    else if ((string)ViewState["Operation"] == "Percentage")
                    {
                        HistoryTextFormatter(6);
                    }
                    else
                    {
                        HistoryTextFormatter(7);
                    }

                }
                catch (FormatException)
                {
                    HistoryTextFormatter(8);
                }
            }
        }

 

Link to post
Share on other sites

How would I then connect the entire equation to the list box in my calculators UI? So that every time the equals button is clicked, it adds to the ListBox that I named LBHistory.

 

MB5AAxG.png

Ultimately everytime I press equals, I would like the list box to look something like this for example:

1 + 1 = 2

2 + 2 = 4

3 + 3 = 6

 

Link to post
Share on other sites

1 minute ago, Sdot said:

How would I then connect the entire equation to the list box in my calculators UI? So that every time the equals button is clicked, it adds to the ListBox that I named LBHistory.

 

MB5AAxG.png

I forgot you had a listbox, I was under the impression that you simply wished to store the string of responses. If that's the case I'd use a different approach, I would bind the listbox to an observable collection so you can view it on real time (you could get by just by creating an array and appending it to the list box but it's better to store them inside a collection if you wish to easily transfer them to a database, export it as a text file, to use it as a source for a grid or if you want to use some filtering).

 

1. First you have to create a class that the collection will use as parameters (declare it below the partial class)

a. ResponseDate = contains the raw date for back-end use

b. ResponseText = contains the raw text for back-end use

c. ListBoxText = the formatted representation of ResponseDate and ResponseText for front-end viewing

        class ResponseClass
        {
            public DateTime ResponseDate { get; set; }
            public string ResponseText { get; set; }
            public string ListBoxText { get; set; }
        }

 

2. Create the Observable Collection (declare it below the partial class)

        ObservableCollection<ResponseClass> ResponseCollection = new ObservableCollection<ResponseClass>();


3. Bind the:

a. Collection to your list box (place this after the InitializeComponent)

b. Choose the ListBoxText from your ResponseClass as the list box's DisplayMemberPath to tell it to show those values as its visual representation for their respective row

            listBox.ItemsSource = ResponseCollection;
            listBox.DisplayMemberPath = "ListBoxText";

Alternatively, you can bind it through XAML

ListBox ItemSource="{Binding ResponseCollection}" DisplayMemberPath="ListBoxText"

 

4. Populate them via the HistoryTextFormatter method

        void HistoryTextFormatter(int responseCode)
        {
            string response;
            DateTime dateNow = DateTime.Now;
            switch (responseCode)
            {
                case 1:
                    response = "No Value is given.";
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 2:
                    response = _Calculate.Add(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 3:
                    response = _Calculate.Subtract(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 4:
                    response = _Calculate.Multiply(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 5:
                    response = _Calculate.Divide(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 6:
                    response = _Calculate.Percentage(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 7:
                    response = "No Operation was recorded.";
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                case 8:
                    response = "Bad Input Format.";
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
                default:
                    response = "Unknown response, contact an admin immediately";
                    Response.Write("<script>alert('" + response + "')</script>");
                    ResponseCollection.Add(new ResponseClass { ResponseDate = dateNow, ResponseText = response, ListBoxText = "[" + dateNow + "]: " + response });
                    break;
            }
        }

 

5. Create the event that will call the HistoryTextFormatter method

        protected void ButtonEquals_Click(object sender, EventArgs e)
        {
            // Switch Response Codes
            // 1 = No Value is Given
            // 2 = Addition
            // 3 = Subtraction
            // 4 = Multiplication
            // 5 = Division
            // 6 = Percentage
            // 7 = No Operation was recorded
            // 8 = Bad input format

            if (calc_result.Value == string.Empty)
            {   // User must enter a value for the button to function correctly.
                HistoryTextFormatter(1);
            }
            else
            {
                ViewState["Value2"] = calc_result.Value;
                calc_result.Value = string.Empty;

                try
                {
                    if ((string)ViewState["Operation"] == "Addition")
                    {
                        HistoryTextFormatter(2);
                    }
                    else if ((string)ViewState["Operation"] == "Subtraction")
                    {
                        HistoryTextFormatter(3);
                    }
                    else if ((string)ViewState["Operation"] == "Multiplication")
                    {
                        HistoryTextFormatter(4);
                    }
                    else if ((string)ViewState["Operation"] == "Division")
                    {
                        HistoryTextFormatter(5);
                    }
                    else if ((string)ViewState["Operation"] == "Percentage")
                    {
                        HistoryTextFormatter(6);
                    }
                    else
                    {
                        HistoryTextFormatter(7);
                    }

                }
                catch (FormatException)
                {
                    HistoryTextFormatter(8);
                }
            }
        }

 

Link to post
Share on other sites

1 hour ago, Sdot said:

How would I then connect the entire equation to the list box in my calculators UI? So that every time the equals button is clicked, it adds to the ListBox that I named LBHistory.

 

MB5AAxG.png

Ultimately everytime I press equals, I would like the list box to look something like this for example:

1 + 1 = 2

2 + 2 = 4

3 + 3 = 6

 

Do you want to only show successful calculations or responses such as bad input format, etc as well?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×