1

I want to dynamically add Checkboxes in a x*y matrix. The simplest way that came to my mind to start a for loop which goes by O(n²). I have 2 TextBoxes which are for the width and height of the matrix. In my example i did 10x10; When i press the button it just creates 1 Checkbox. I first tried to directly add the Checkbox to the panel but i somehow got a NullReferenceException. Now i am on a List which fills in the for loop and gets read out afterwards in the foreach loop.

Any help would be appreciated.

Thanks in advance

m0ddixx

My Try on this:

namespace LED_Matrix_Control
{
public partial class Form1 : Form
{
    private LedMatrix ledMatrix;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int width= Convert.ToInt32(breiteFeld.Text);
        int height = Convert.ToInt32(hoeheFeld.Text);
        List<CheckBox> ledchecks = new List<CheckBox>();
        ledMatrix = new LedMatrix(breite, hoehe);
        for(int x = 0; x < breite; x++)
        {
            for(int y = 0; y < hoehe; y++)
            {
                ledchecks.Add(addCheckBox(x, y));
            }
        }
        foreach(CheckBox finalLedChk in ledchecks)
        {
            panel1.Controls.Add(finalLedChk);
        }
    }
    private CheckBox addCheckBox(int x, int y)
    {
        CheckBox ledcheck = new CheckBox();
        ledcheck.Location = new Point(x, y);
        return ledcheck;
    }
}
}
m0ddixx
  • 13
  • 2
  • What problem do you have with this code? I you want to be helped I suggest to always describe the problem as you experience it. By the way, the Location of your CheckBoxes are too tight. Add a multiplier to X and Y – Steve Nov 17 '16 at 18:10
  • OK. The problem is to have a dynamic matrix of checkboxes to replicate an led matrix. Obviously these leds can be on or off. the program itself should generate code to create animations based on keyframes. but first the user has to put in the width and the height of that matrix and then it should generate x*y checkboxes on runtime. in my case it only creates one checkbox. – m0ddixx Nov 17 '16 at 18:14
  • Change this line to: _ledcheck.Location = new Point(x * 20, y * 20);_ Your checkboxes are there, they are just one over the other. – Steve Nov 17 '16 at 18:16
  • ok as of now it just creates 10 checkboxes in the y coloumn. the x width is ignored or something. so its 1 dimensional – m0ddixx Nov 17 '16 at 18:17

1 Answers1

0

If you panel is big enough to host all controls then you have just a simple problem. You are stacking all the created checkboxes roughly at the same location.

The checkbox is probably the smallest control (together with the radiobutton) but notwistanding this they have a size and if you want to see them you should position them in different enough location

You code don't requires two loops. You could write something like this

    for(int x = 0; x < breite; x++)
    {
        for(int y = 0; y < hoehe; y++)
        {
            CheckBox ledcheck = new CheckBox();
            ledcheck.Location = new Point(x * 20, y * 20);
            ledcheck.Size = new Size(15,15);
            panel1.Controls.Add(ledcheck);
        }
    }

Consider also to explore the use of a TableLayoutPanel. This control provides some form of grid to help you to automatically position your checkboxes.

For example

Form f = new Form();
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.RowCount = 5;  // <= this should come from user input 
tlp.ColumnCount = 5; // <= this should come from user input 

tlp.Dock = DockStyle.Fill;

for (int x = 0; x < 5; x++)
{
    for (int y = 0; y < 5; y++)
    {
        CheckBox ledcheck = new CheckBox();
        // No need to position the checkboxes.....
        // ledcheck.Location = new Point(x * 20, y * 20);
        ledcheck.Size = new Size(15,15);
        tlp.Controls.Add(ledcheck);
    }
}
f.Controls.Add(tlp);
f.Show();
Steve
  • 213,761
  • 22
  • 232
  • 286
  • That's weird. Or your panel is too small in the X axys or the x is not multiplied by 20 – Steve Nov 17 '16 at 18:30
  • 2
    Actually the size was the problem. after setting ledcheck.size = new Size(15,15) it worked. the checkboxes hat an invisible text field next to each other so it overlapped. thanks for help anyways! – m0ddixx Nov 17 '16 at 18:35
  • Added your findings to the answer – Steve Nov 17 '16 at 18:38