.NET 2.x Динамическое добавление кнопок на форму - C#

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

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

Чет не пойму почему не выводятся кнопки на форму, до этого когда не было метода call_button_filling() а цикл и добавление кнопок происходило по событию Form2_Load всё работало.... Объясните где затупил ?
namespace WindowsFormsApplication5
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();    
        }
        private void Form2_Load(object sender, EventArgs e)
        {
           form_filling ff = new form_filling();
           ff.call_button_filling(); 
        }
    }
    public partial class form_filling : Form
    {
        public void call_button_filling()
        {
            for (int k = 1; k <= 9; k++)
            {
                Control a = button_filling(k);
           Controls.Add(a);
            }
        }
        private Control button_filling(int k)
        {
            Button button = new Button();
                button = new Button();
                button.Name = "bt"+k;
                button.Size = new System.Drawing.Size(50, 45);
                if (k <= 3)
                {
                    button.Location = new System.Drawing.Point(50*(k), 50);
                    return button;
                }
              if (k <= 6)
                {
                    button.Location = new System.Drawing.Point(50*(k-3), 50*2);
                    return button;
                }
                button.Location = new System.Drawing.Point(50*(k-6), 50*3);
                return button;
        }
    }
}
И за одно расскажите как нормальные люди в этой ситуации без трёх условий IF обошлись бы

Решение задачи: «.NET 2.x Динамическое добавление кнопок на форму»

textual
Листинг программы
namespace SmartDeviceProject4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void Form2_Load(object sender, EventArgs e)
        {
            form_filling.call_button_filling(this);   
            form_filling.textb_filling(this);
        }
    }
 
    public partial class form_filling
    {
        public static void call_button_filling(Control sender)    
        {
            for (int k = 0; k <= 11; k++)
            {
                sender.Controls.Add(button_filling(k));
            }
        }
 
        private static Control button_filling(int k)
        {
            Button button = new Button();
            button = new Button();
            button.Name = "bt" + k;
            button.Size = new System.Drawing.Size(50, 45);
            button.Location = new System.Drawing.Point((k % 3 + 1) * 50, (k / 3 + 1) * 50);
            return button;
        }
 
        public static void textb_filling(Control sender_2)
        {
            TextBox textb = new TextBox();
            textb.Name = "textb";
            textb.Size = new System.Drawing.Size(100, 100);
            textb.Location = new System.Drawing.Point(100,100);
            sender_2.Controls.Add(textb);
          
        }
    }
}

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

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