C# calculator and keyboard interaction

Asked

Viewed 242 times

1

Guys, here’s what I’m doing: I’m doing a C# and I wanted to put in a root calculus. The problem is that the calculation is only done with the value I put in the variable first. I can not put this part of the code to calculate a value I type on the keyboard or I click on the screen. Someone can help?

 private void buttonRaiz_Click(object sender, EventArgs e)
    {
        double primeiro = 2;          

        double resutado = Convert.ToSingle(Math.Sqrt(primeiro));

        Tela.Text = resutado.ToString();

    }       
  • It’s not clear.

1 answer

1

Sidsan,

If you are using Windows Forms you need to put a Textbox on the screen as well. Then you associate the textbox value into your "first" variable. Example:

double primeiro;
primeiro =  Convert.ToDouble(textBox1.Text);

For your Textbox to accept only numbers, because if the user type a letter will appear an error at runtime, within the event Keypress must have the following code:

Private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && e.KeyChar != 8) e.Handled = true;
}

Now I’ll put some links for you to read:

C# Textbox Control

Textbox Class - System.Windows.Forms

C# - Resizing a Textbox

[]'s,

Fabio I.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.