0

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!

Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
Zirc
  • 460
  • 3
  • 14

1 Answers1

0

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]);
David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • I was able to create a new button with your corrected syntax, but there was an error in the `"this->Children->Add(matrix[oucounter, incounter]);"` 'which is **class "CppWinForm1::MyForm"has no member "Children"**. I think that is the problem why I can't put the button in the flow layout panel. – Zirc Oct 10 '16 at 15:26
  • The code is working now, but the button is not in the flow layout panel which makes the buttons be in the same place. See [picture](https://drive.google.com/open?id=0B1Dv8zquYx6BaXE0aG94Ny0zdGM) – Zirc Oct 10 '16 at 15:35
  • Try adding it to the flow layout panel, instead of directly to the form. You'll probably need to set some other properties on the buttons and/or on the flow layout panel. If you're still having problems, ask another question more specifically about layout, rather than syntax errors. (You're supposed to only ask one question per post.) – David Yaw Oct 10 '16 at 15:43