简体   繁体   中英

How to call two methods from one button on a WPF form (C#)

How do I code this in a way that allows me to run two methods when one button is clicked on a form.

Run CalculateArea() and CalculatePerimeter() when I click btnCalculate.

I'm not really sure what I'm missing here, am I putting something in the wrong place or am I missing something.

    using System;
    using System.Windows.Forms;

    namespace PracticalExam1
    {
        public partial class frmMain : Form
        {

            public frmMain()
             {
                InitializeComponent();

            Rectangle r = new Rectangle();

        }

        public class Rectangle
        {
            public float Length;
            public float Width;
            public float Area;
            public float Perimeter;

        }

        public void frmMain_Load(object sender, EventArgs e)
        {

        }

        public void CalculateArea(float length, float width, float area)
        {
            area = length * width;
            txtArea.Text = Convert.ToString(area);
        }       

        public void CalculatePerimeter(float length, float width, float perimeter)
        {
            perimeter = length * 2 + width * 2;
            txtPerimeter.Text = Convert.ToString(perimeter);
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            CalculateArea();
            CalculatePerimeter();
        }
    }
}

That's how i wrote the CalculateArea method...

private double CalculateArea(double h, double b)
{
  return h * b;
}

double area = CalculateArea(12.5, 20);

You don't need to pass the area argument... just return the result and assign it to a variable. If you understood what I am saying, you should be able to do the CalculatePerimeter method too.

Take a break coding and revise C# methods and variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM