Two Dem Button Array Population

I have a 100 buttons that I have already have created they are button1, button2 and so on. I have created a two dem button array. I was wondering what would be the best way to populate the button array for instance

button 1 -10
button 11 - 20
button 21 - 30

and so on. this how I am trying to achieve this.

1
2
3
4
5
6
7
8
9
10
11
void Array_Population()
		{
			array<Button^, 2>^ btn = gcnew array<Button^, 2>(10, 10);
			for (int x = 0; x < 10; ++x)
			{
				for(int y = 0; y < 10; ++y)
				{
					btn[x, y] = button1;
				}
			}
		}
Last edited on
Uh, CLI questions should probably be posted in the Windows Programming forums only. Technically it isn't even considered C++ but rather its own language.

Please don't double post.
http://www.cplusplus.com/forum/windows/214534/
Last edited on
 have a 100 buttons that I have already have created

in what sort of container are these buttons held – c-style array / std::array / std::vector / something else?
In any case consider a std::vector<std::vector<Button>> for the 2D holding container for buttons and each element of the 2d vector (which is a std::vector<Button>) can be initialized with the appropriate ranges of the container that holds the Button instances
seeing you're using std::array for the 2d container, let's assume that the 100 buttons are also held in an std::array, so in that case the program would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <array>
#include <iterator>
#include <iomanip>

struct Button
{
    size_t m_ID;
};

constexpr auto SIZE = 10;

int main()
{
    std::array<Button, SIZE * SIZE > arrayButton{};
    for (size_t i = 0; i < SIZE * SIZE; ++i)arrayButton[i] = Button{i+1};

    std::vector<std::vector<Button>> vectorButton{};

    for (size_t i = 0; i < SIZE*SIZE; i += SIZE)
    {
       vectorButton.emplace_back(std::make_move_iterator(arrayButton.begin() + i),
            std::make_move_iterator(arrayButton.begin() + i + SIZE));
    }
    for (const auto& elem : vectorButton)
    {
        for (const auto& elemI : elem)
        {
            std::cout << std::left << std::setw(5) << elemI.m_ID << " ";
        }
        std::cout << "\n";
    }
}

note that contents of the std::array are being moved, not copied, into the 2d std::vector which means that the std::array is left in a stable but undefined state after the op and it's elements cannot be re-used. If there is future need for the std::array elements then, of course, it should be copied not moved
Last edited on
Topic archived. No new replies allowed.