A
A
Anatoliy53542018-10-01 23:16:13
C++ / C#
Anatoliy5354, 2018-10-01 23:16:13

How to fix a bug in a simple C# program?

It is necessary to make a calculator program in C # in Windows form. The program contains the text "Enter value", after which the value is entered in the field
. Then after pressing the "calculate" button, you need to display the value calculated by the formula.
I almost wrote the program, but when I try to enter any value in textBox1, near the line x1 = Double.Parse(x); - the error "System.ArgumentNullException: Value cannot be null.
Parameter name: value" is generated.
Perhaps the program itself is completely incorrect, but this is only the 2nd pair of programming and tomorrow you need to put it in the line, please help. Thanks in advance!
Skin below and screen

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        double x1;
        double y;
        string x;
       public Form1()
        {
            InitializeComponent();
        }

        
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
             x = Console.ReadLine();
             x1 = Double.Parse(x);

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            double x2;
            x2 = x1 * 3;

            y = Math.Pow(Math.Cos(Math.Exp((x1 + 2 * x2 + 9) / 0.666)), 3);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox2.Text = y.ToString();
        }
    }
}

5bb280ac21632816132196.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tex0, 2018-10-02
@tex0


you are trying to access the windows console environment in a windowed application :-)
you need to do this:
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox == null) return; //или обрабатывайте по своему данную ситуацию
            if (!Double.TryParse(textBox.Text, out x1) return; //или обрабатывайте по своему данную ситуацию, например выводите сообщение о том что формат ввода не верен
        }

M
Maxim Isaev, 2018-10-02
@MaximIs

I would write
double x1 = Convert.ToDouble(textBox1.Text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question