my code is:

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        double num;
        string text = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void calFactorial_TextChanged(object sender, EventArgs e)
        {
            if (calFactorial.Text != "")
            {
                if (!(Char.IsNumber(calFactorial.Text, 0)))
                {
                    factLabel.Text = "Enter number only";
                }
                else if (Convert.ToDouble(calFactorial.Text) == 0)
                {
                    factLabel.Text = "Please provide a Number greater than 0.";
                }                           
            }
            else
            {
                factLabel.Text = "Please Enter Number";
            }
        }

        private void factorial_Click(object sender, EventArgs e)
        {
            if (calFactorial.Text == "")
            {
                factLabel.Text = "Please enter a number";
            }                      

            else
            {    
                num = Convert.ToDouble(calFactorial.Text);
                RichTextBox richTextBox = new RichTextBox();                
                foreach (Control removeControl in Controls)
                {
                    removeControl.Hide();
                    calFactorial.Show();
                    factLabel.Show();
                    factorial.Show();
                }
                Button clear = new Button();
                clear.Location = new Point(500, 150);
                richTextBox.Location = new Point(100, 100);
                richTextBox.Size = new Size(300, 200);
                clear.Text = "Clear";
                this.Controls.Add(clear);
                this.Controls.Add(richTextBox);
                double fact = 1;               
                for (double i = num; i >= 1; i--)
                {
                    fact = fact * i;
                }
                richTextBox.Text = text + "The factorial of " + Convert.ToString(num) + " is: " + Convert.ToString(fact) + "\n";
                text = richTextBox.Text;
                clear.Click += new EventHandler(ClearClick);
                richTextBox.TextChanged += new EventHandler(ClearClick);             
            }
        }

        private void ClearClick(object sender, EventArgs e)
        {            
            foreach (Control removeControl in Controls)
            {
                removeControl.Hide();
                calFactorial.Show();
                factLabel.Show();
                factorial.Show();
                calFactorial.Text = "";
            }

        }

    }
}

nw tell me how to reset contents of richtextbox on clicking clear button....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.