I'm making a gridbox that consist of 9x9 buttons in a Flow layout panel.
I learned that a Flow layout panel can auto-arrange and auto-size the buttons that I will be adding. I also learned that I can create an array of buttons digitally by this code
cli::array<Button^, 2>^ matrix = gcnew cli::array<Button^, 2>(9, 9);
which creates a two-dimensional array of buttons that consist of 9x9 elements, but I want to ask how do I display it in the interface?
I had an idea of something like this
private: System::Void Area_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
//"Area" is the name of the Flow layout Panel
cli::array<Button^, 2>^ matrix = gcnew cli::array<Button^, 2>(9, 9);
for (int oucounter = 0; oucounter < 9; oucounter++)
{
for (int incounter = 0; incounter < 9; incounter++)
{
matrix[oucounter][incounter]->Parent = this; //error
matrix[oucounter][incounter]->Text = "0"; //error
}
}
}
Although, I'm having an error "invalid number of subscripts for this cli::array type".
I also want to add controls in the buttons. Whenever I click a specific button, I want it to have an incrementing value on its number being displayed.
Any help would be appreciated. Also, please let me know if my starting codes are incorrect in some way. Thanks!
invalid number of subscripts for this cli::array type
matrix[oucounter][incounter]
What you have there is access two arrays, an outer array then an inner array, but what you have declared is a single 2D array. For that, the syntax is:
matrix[oucounter, incounter]
If you want to display these on the UI, you'll need to first create the Button objects.
matrix[oucounter, incounter] = gcnew Button();
I'm not a WinForms expert, but I believe the standard way to insert the button into a form isn't to set the parent of the button, but rather to add the button to the list of the form's controls.
this->Controls->Add(matrix[oucounter, incounter]);