-1

I am trying to get this code to work so when a numbers are put in the length and width text boxes they are calculated to come up with the area and perimeter. This is for a class assignment and my teacher is telling me that I am not correctly displaying the calculated results in the output text boxes. I would love it if someone tell me how to edit my code so that it will run correctly. Thank you.

private void btnCalculatE_Click(object sender, EventArgs e)
{
    double Length;
    double Width;
    double txtArea;
    double txtPerimeter;


    Length = Convert.ToDouble(txtLength.Text);
    Width = Convert.ToDouble(txtWidth.Text);


    txtArea = Length * Width;
    txtPerimeter = 2 * (Length + Width);


    txtArea = Convert.ToDouble(txtArea);
    txtPerimeter = Convert.ToDouble(txtPerimeter);

    txtLength.Focus();
}

private void btnExiT_Click(object sender, EventArgs e)
{
    this.Close();
}
xsami
  • 1,312
  • 16
  • 31
lanc
  • 1
  • 1
  • 1
    where is the code that shows you updating the output text boxes? – David Green Jun 13 '17 at 02:05
  • I havent updated the text boxes. The code i have displayed is all that I have. I am new to this and I am teaching myself so I am having a hard time knowing what is right and wrong. – lanc Jun 13 '17 at 02:08
  • @lanc, perhaps this answer here could help you better [c# put string into textbox](https://stackoverflow.com/questions/1217772/c-sharp-put-string-into-textbox) – Kyojimaru Jun 13 '17 at 02:19
  • @lanc you said you haven't updated the text boxes, so how does your teacher know that you are incorrectly displaying the calculated result in the output textbox? – grepLines Jun 13 '17 at 02:28
  • @Ianc you are not updating your textbox with calculated value. Just set the calculated values to your textbox /label where you want to display them using there Property `.Text` – Mahesh Jun 13 '17 at 02:36
  • I have no idea. I sent him my code and he said that I am not correctly displaying the results. I am a little confused. – lanc Jun 13 '17 at 02:38

2 Answers2

0

I assume that you you use TextBox.Text and you have 2 output text boxes named textArea and textPerimeter.

private void btnCalculatE_Click(object sender, EventArgs e)
{
    double Length;
    double Width;   
    double textAreaDouble;
    double textPerimeterDouble; 

    //Length & Width now are double 
    Length = Convert.ToDouble(txtLength.Text);
    Width = Convert.ToDouble(txtWidth.Text);

    txtAreaDouble = Convert.Length * Width;
    txtPerimeterDouble = 2 * (Length + Width);

    //Set new values in your textboxes 
    textArea.Text = textAreaDouble.ToString();
    textPerimeter.Text = textPerimeterDouble.ToString(); 

    txtLength.Focus();
}
grepLines
  • 2,478
  • 3
  • 21
  • 32
  • I took out the line of code you said I do not need and nothing happened. When I run it nothing calculates – lanc Jun 13 '17 at 02:52
  • the new changes will not change the output of the code, it's just remove redundant code. I need more information from you: 1. What does your program produce at the moment? 2. What is the full class of the your textboxes? – grepLines Jun 13 '17 at 02:58
  • and why do you call `txtLength.Focus();`? – grepLines Jun 13 '17 at 03:00
  • Focus selects the text box (so the cursor will show up there) – Apidcloud Jun 13 '17 at 03:27
0

If you name the textboxes that display the Area and the Perimeter to txtArea and txtPerimeter, then this is the code you need:

private void btnCalculatE_Click(object sender, EventArgs e)
{
    double Length;
    double Width;

    Length = Convert.ToDouble(txtLength.Text);
    Width = Convert.ToDouble(txtWidth.Text);

    txtArea.Text = Convert.ToString(Length * Width);
    txtPerimeter.Text = Convert.ToString(2 * (Length + Width));

    //The following has nothing to do with calculating the result.
    //You may leave it if you intend to set focus to the *Length* textbox after calculating.
    txtLength.Focus();
}

Hope that helps.