I am working on a project in C# and I have to make console application and also windows forms application. I have completed the console application, but now I have a problem with "converting" it into windows forms. The application have to count euler number from infite series. My problem is that when I start it, it freezes and won't give me any output in the textbox. The output I want is of course numeric. Here is the code itself. I hope that it wouldn't be a problem to understand it without the gui.
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 Eulerovo_cislo_windows_forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int konst = Int32.Parse(textBox1.Text);
double euler = 1;
int variable = 0;
int index = 0;
int factorial = 0;
double absolute_error;
variable = konst;
richTextBox1.Text = ("---ongoing calculations---");
while (true)
{
try
{
if (konst < 1 || konst > 30) throw new ArgumentOutOfRangeException("you have not entered a value between 1 and 30");
while (variable > 0)
{
if (variable == 1)
factorial = 1;
else
{
index = variable - 1;
faktorial = variable;
while (index > 0)
{
factorial *= index;
index--;
}
}
promenna--;
euler += Convert.ToDouble(1) / factorial;
richTextBox1.Multiline = true;
richTextBox1.Text = euler.ToString();
}
}
catch (ArgumentOutOfRangeException ae) // exceptions
{
MessageBox.Show(ae.Message);
break;
}
catch (FormatException fe)
{
MessageBox.Show(fe.Message);
break;
}
finally
{
absolute_error = EulerNumberError.error(euler);
textBox3.Text = absolute_error.ToString();
textBox2.Text = euler.ToString();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
class EulerNumberError // absolute error
{
public static double error(double a)
{
return Math.E - a;
}
}
}