Программа не идет по указанному пути - C#

Узнай цену своей работы

Формулировка задачи:

Программа не идет по указанному пути или я что-то не понимаю... программа для действий с матрицами. BuildMatr()-создает и выводит на форму матрицу сначала все работает, НО при нажатии на кнопку Clear на форме матрицы уничтожаются и можно работать с новыми, А Когда программа доходит до public void button1_Click(object sender, EventArgs e){ .... M.Click += new System.EventHandler(MClickEventHandler);} она не идет в метод MClickEventHandler, а почему то возвращается один раз в public void button1_Click(object sender, EventArgs e) и уже потом идет в MClickEventHandler Не пойму почему, помогите пожалуйста понять что исправить... Вот код
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 _123456
{
    public partial class Form1 : Form
    {
        TextBox Operator = new TextBox();
        List<TextBox> tb = new List<TextBox>();
        public int PorMatr;
        public Form1()
        {
            InitializeComponent();
        }
        public void BuildMatr(int X, int Y,int k)
        {
            for (int j = 0; j < PorMatr; j++)
            {
                for (int i = 0; i < PorMatr; i++)
                {
                    var temp = new TextBox();
                    temp.Name = "tb"+k.ToString();
                    temp.Tag = i+j+k;
                    temp.Location = new Point(X, Y + i * 30);
                    temp.Size = new Size(46, 20);
                    this.Controls.Add(temp);
                    tb.Add(temp);
                    k = k + 1;
                }
                X = X + 50;
            }
       }
        public void button1_Click(object sender, EventArgs e)
        {
            PorMatr = (int)numericUpDown1.Value;
            BuildMatr(86, 100,0);
            Operator.Size = new Size(46, 20);
            Operator.Location = new Point(300, 120);
            Operator.Name = "Operator";
            //Operator.TabIndex = 1;
            Controls.Add(Operator);
            Button M = new Button();
            M.Text = "M";
            M.Location = new Point(300, 144);
            M.Name = "M";
            M.Size = new Size(40, 25);
            Controls.Add(M);
            M.Click += new System.EventHandler(MClickEventHandler); 
       }
        public void MClickEventHandler(object Sender, EventArgs e)
        {
            PorMatr = (int)numericUpDown1.Value;
            BuildMatr(402, 104, PorMatr * PorMatr);
            Button Ravno = new Button();
            Ravno.Text = "=";
            Ravno.Location = new Point(600, 144);
            Ravno.Name = "Ravno";
            Ravno.Size = new Size(40, 25);
            Controls.Add(Ravno);
            Ravno.Click += new System.EventHandler(RClickEventHandler);
        }
        public void RClickEventHandler(object Sender, EventArgs e)//=
        {
            PorMatr = (int)numericUpDown1.Value;
            BuildMatr(652, 104, 2*PorMatr * PorMatr);
            float[] Arr = new float[3 * PorMatr * PorMatr];
            Single numTextBox;
            for (int i = 0; i < 2 * PorMatr * PorMatr; i++)
            {
                Boolean isnum = Single.TryParse(tb[i].Text, out numTextBox);
                Arr[i] = numTextBox;
            }
            if (Operator.Text == "+")
            {
                for (int j = 0; j < PorMatr * PorMatr; j++)
                     Arr[j + 2 * PorMatr * PorMatr] = Arr[j] + Arr[j + PorMatr * PorMatr];
            }
            if (Operator.Text == "-")
            {
                for (int j = 0; j < PorMatr * PorMatr; j++)
                     Arr[j + 2 * PorMatr * PorMatr] = Arr[j] - Arr[j + PorMatr * PorMatr];
            }
            if (Operator.Text == "*")
            {
                for (int k = 0; k < PorMatr * PorMatr; k++)
                {
                    float S = 0;
                    int m = k % PorMatr;
                    int l = k / PorMatr;
                    for (int i = 0; i < PorMatr; i++)
                        S = S + Arr[PorMatr * i + m] * Arr[l * PorMatr + PorMatr * PorMatr + i];
                    Arr[2 * PorMatr * PorMatr + k] = S;
                }
            }
            if (Operator.Text == "/")
            { }
            for (int j = 0; j < PorMatr * PorMatr; j++)
                tb[j + 2 * PorMatr * PorMatr].Text = Arr[j + 2 * PorMatr * PorMatr].ToString();
        }
        public void button2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
        public void button3_Click(object sender, EventArgs e)
        {
            int PorMatr = (int)numericUpDown1.Value;
            tb.RemoveRange(0, 3 * PorMatr * PorMatr);
            for (int i = 0; i < 3 * PorMatr * PorMatr + 3; i++)
                this.Controls.Remove(Controls[this.Controls.Count - 1]);
            Operator.Text = "";
            button1.Click += new System.EventHandler(button1_Click);
        }
    }
}

Решение задачи: «Программа не идет по указанному пути»

textual
Листинг программы
textBox1.Text = String.Empty;

Оцени полезность:

10   голосов , оценка 4.4 из 5
Похожие ответы