I have been given an assignment on C# window form where I get to calculate an input textbox from a user, the user wants to input a value and then presses a radiobutton(square root) then clicks a normal button to get the result. Is it possible to get the result in a textbox (instead of a label)? If so, how do I do that using 'methods' (linking methods to the button_click one)? Also, why do I need to return a parameter value of a method? it doesn't let me execute without returning ;/
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int square(int A)
{
if (rbSquare.Checked)
{
Math.Sqrt(A);
return Convert.ToInt32(Math.Sqrt(A));
}
else
{
return A;
}
}
private void btncalculate_Click(object sender, EventArgs e)
{
int firstNum = Convert.ToInt32(tbinput.Text);
int squareRoot = square(firstNum);
tbResult.Text = "" + squareRoot;
}
}
}
(int)Math.Sqrt(A);
rather thanConvert.ToInt32
; it is more idiomatic in C# to use a cast here.