Help Me Solve This. Write a C# GUI application that finds the square root of an
ID: 3685671 • Letter: H
Question
Help Me Solve This.
Write a C# GUI application that finds the square root of an integer value entered by the user. The Math class contains a static method named Sqrt() that accepts a parameter and returns the parameter’s square root. If the user’s input value cannot be parsed to int, display the programmer-defined message shown. If the user’s input value is a negative value, throw a new ApplicationException to which you pass the programmer-defined message shown. If the user’s input value is a positive integer, pass it to the Math.Sqrt() method and display the returned value to four decimal places. The findButton is to be designated as the Accept button. The exitButton is to be designated as the Cancel button. Access Keys are to be assigned to all buttons on the GUI. Set the Tab index to a logical order. Design your GUI as shown.
Create a Click event method for the findButton. Within the method, create the necessary variables. Create the try block that contains an if-else statement to determine if the user’s input value can successfully parsed to int. If parsed successfully, calculate and display the square root using the square root method shown above. Otherwise, display the entire “not a type int number” message. Also, within the Try block, create an if statement that determines if the user entered a negative integer number. If a negative integer number was entered, throw a new ApplicationException and pass it the entire “can’t be negative” message. Create a catch block to handle the ApplicationException by displaying the thrown message text. Set the focus to the number text box. Select all text in the number text box. Items in the display label are to appear as shown.
Create a TextChanged event method for the number text box. Its only task is to clear the contents of the display label.
Create a Click event method for the exitButton that terminates the application.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace sqrt_cal_using_exception
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double s;
int a;
try
{
a = Int32.Parse (textBox1.Text);
if (a < 0)
{
throw (new ApplicationException ("Given data is not positive integer"));
}
if (a > 0)
{
s = Math.Sqrt(a);
label2.Text = s.ToString();
}
}
catch (FormatException)
{
label2.Text = "Non Numeric value";
}
catch (ApplicationException )
{
label2.Text = "Negative Value ";
}
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.