Home > Back-end >  Call method inside a Form
Call method inside a Form

Time:03-22

Hi I am learning C# and struggling with a Form in which a btnCalc_click event should call a method calcArea and produce the output in a textBox1.text

the error is in row: textBox1.Text = calcArea.ToString();

who can help with with the correct syntax ?

namespace Meetkunde
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void txbLengte_TextChanged(object sender, EventArgs e)
        {
            double length = 0;
            length = double.Parse(txbLength.Text);
        }

        private void txbBreedte_TextChanged(object sender, EventArgs e)
        {
            double width = 0;
            width = double.Parse(txbWidth.Text);
        }

        public double calcArea(double length, double width)
        {
            double area = 0;
            area = (length * width);
            return area;
        }

        private void label3_Click(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {         
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            textBox1.Text = calcArea.ToString();
        }
    }
}

CodePudding user response:

As I mentioned above - you need to pass the method the parameters as you defined them in the method. You don't need Text Changed events for this either - plus they aren't doing anything. Try changing your code to something like (not real sure of what exactly you named textboxes):

 private void btnCalc_Click(object sender, EventArgs e)
   {
       double width = Convert.ToDouble(txbBreedte.Text);//txbWidth.Text?              
       double length= Convert.ToDouble(txbLengte.Text);//txbLength.Text?
       textBox1.Text = calcArea(length, width).ToString();
  }

CodePudding user response:

calcArea.ToString();

when you are calling this, you aren't calling the function, you are referencing the function, not the return

do

calcArea(parameters).ToString();

replace parameters with what you want to calculate.

  • Related