A
A
Anatoliy53542018-10-28 12:15:06
C++ / C#
Anatoliy5354, 2018-10-28 12:15:06

How to remake a console application for Windows Forms in C#?

There is a very simple C# console program. It does the following: One numeric value X is entered from the keyboard. After pressing the enter button, the program reads the result according to the formula and displays it on the screen. It contains "fool protection", if you enter not a number, then it writes like "the value entered is incorrect, try again." For the next couple, you need to implement everything the same, only in a window (windows forms).
The windows form has label1(Enter X value), textBox1(to enter value), button(Get result), and textBox2(where result is displayed). The program should work like this: from the keyboard (an integer or fractional number is entered into textBox1), after pressing the button, the result is displayed in textBox2. If the value is entered incorrectly, an error message is displayed in the second (small) window.
Please help me write the program code. I can't figure out the event handlers in any way, and protection for a fool is done in a completely different way ... I have a ready-made window, only the code is needed. (window attached below).
Console application code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3333
{
    class Program
    {
        static void Main(string[] args)
        {
            double x1;
            while (true)
            {
                Console.Write("Введите значение X = ");
                if (double.TryParse(Console.ReadLine(), out x1))
                {
                    break;
                }
                else
                {
                    Console.Write("Значение введено неверно \n ");
                }
         
            } 

            double x2 = x1 * 3;
            double y;

            y = Math.Sqrt(56 + ((x1 + x2 + Math.Sin(x1 * x2)) / (5 * Math.Cos(Math.Pow(x2, 2)))));
            Console.WriteLine("Х равен" + x1);
            Console.WriteLine("Значение " + y);
            Console.ReadKey();

        }

    }
}

5bd57e15db38c694312112.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2018-10-28
@Morpheus_God

On the button click event, write a similar function.

double x1;

            if(double.TryParse(textBox1.Text,out x1))
            {
                double x2 = x1 * 3;
                double y;

                y = Math.Sqrt(56 + ((x1 + x2 + Math.Sin(x1 * x2)) / (5 * Math.Cos(Math.Pow(x2, 2)))));
                textBox2.Text = $"Х равен {x1} Значение y {y}";
            }
            else
            {
                MessageBox.Show("Значение введено не верно!!!");
                textBox1.Text = "";
                textBox2.Text = "";
            }

I
Ivan Vyrov, 2018-10-28
@ProKiLL

Here is the solution for you (insert in the button click event):

if (double.TryParse(textBox1.Text, out x1))
            {
                x2 = x1 * 3;

                y = Math.Sqrt(56 + ((x1 + x2 + Math.Sin(x1 * x2)) / (5 * Math.Cos(Math.Pow(x2, 2)))));
                textBox2.Text = $"Х равен {x1.ToString()},  Значение y {y.ToString()}";
            }
            else
            {
                MessageBox.Show("Значение введено неверно \n ");
            }

do not forget to change textBox1 to the name of field 1 and textBox2 to the name of field 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question