CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2006
    Posts
    11

    Unhappy Creating array of buttons in Visual C++

    Dear All

    I have code in C# program:

    int myHeight=30;
    int myWidth=90;
    Button[,] ba = new Button[2, 2];
    for (int i=0; i<2;i++)
    for (int j = 0; j < 2; j++)
    {
    ba[i, j] = new System.Windows.Forms.Button();
    ba[i, j].Height = myHeight;
    ba[i, j].Width = myWidth;
    ba[i, j].Location = new Point( i * myWidth,j * myHeight);
    ba[i, j].Text = i.ToString() + j.ToString();
    panel1.Controls.Add(ba[i, j]);
    }

    How write it in VC++?

    My problem is
    Button[,] ba = new Button[2, 2];
    I do not know how write it in VC++

    thank you

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating array of buttons in Visual C++

    The first question is - why do you need7wnat to create this array in the heap?
    Why not just set the class member variable:
    Code:
    CButton m_arButton[2, 2];
    Then you could create the button controls using CButton::Create method.
    See MSDN for details.

  3. #3
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: Creating array of buttons in Visual C++

    Also MSDN has a couple of good Tutorials in VC++
    - Sreehari
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
    " Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin

  4. #4
    Join Date
    Dec 2006
    Posts
    11

    Re: Creating array of buttons in Visual C++

    can you do it without MFC

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating array of buttons in Visual C++

    Sure! but it would be more complex than using MFC.
    Create a Win32 project.
    Declare the array:
    Code:
    HWND hwndButton[2, 2,];
    And read MSDN in "Platform SDK: Windows User Interface" section about buttons, button creating and using:
    Buttons
    Using Buttons

  6. #6
    Join Date
    Dec 2006
    Posts
    11

    Re: Creating array of buttons in Visual C++

    this code

    CButton m_arButton[2, 2];
    and
    HWND hwndButton[2, 2,];

    does not work without new *.h files.

    this one is working
    Button Bbutt ;
    but i need more
    i trying to use
    Button Bbutt [2];
    but

    ------ Build started: Project: pka, Configuration: Debug Win32 ------
    Compiling...
    pka.cpp
    d:\alex\projects\pka\pka\Form1.h(366) : error C2728: 'System::Windows::Forms::Button' : a native array cannot contain this managed type
    Did you mean 'array<System::Windows::Forms::Button>'?
    d:\alex\projects\pka\pka\Form1.h(366) : error C2227: left of '->{ctor}' must point to class/struct/union/generic type
    Build log was saved at "file://d:\alex\Projects\pka\pka\Debug\BuildLog.htm"
    pka - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    My project use standard windows libraries, without ATL

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating array of buttons in Visual C++

    Quote Originally Posted by alexon1ine
    this code

    CButton m_arButton[2, 2];
    and
    HWND hwndButton[2, 2,];

    does not work without new *.h files.
    What do you mean?
    Have you created a new VC++ project? Which type of project?
    this one is working
    Button Bbutt ;
    but i need more
    i trying to use
    Button Bbutt [2];
    What is Button ? Is ot still c# ?
    Did you look at MSDN sample code in the link I had posted you?
    but

    ------ Build started: Project: pka, Configuration: Debug Win32 ------
    Compiling...
    pka.cpp
    d:\alex\projects\pka\pka\Form1.h(366) : error C2728: 'System::Windows::Forms::Button' : a native array cannot contain this managed type
    Did you mean 'array<System::Windows::Forms::Button>'?
    d:\alex\projects\pka\pka\Form1.h(366) : error C2227: left of '->{ctor}' must point to class/struct/union/generic type
    Build log was saved at "file://d:\alex\Projects\pka\pka\Debug\BuildLog.htm"
    pka - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    My project use standard windows libraries, without ATL
    IWhat is "System::Windows::Forms::Button"?
    Is it still c#?

  8. #8
    Join Date
    Dec 2006
    Posts
    11

    Re: Creating array of buttons in Visual C++

    No it's not C#, it's C++
    buty form from C#

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating array of buttons in Visual C++

    Quote Originally Posted by alexon1ine
    No it's not C#, it's C++
    buty form from C#
    Sounds like this is a problem (form from C# in the c++ project)!

  10. #10
    Join Date
    Dec 2006
    Posts
    11

    Re: Creating array of buttons in Visual C++

    thanks for all of your for help
    I need learn more

    Anyone used the array of the buttons in C++?
    If yes, plz write code with declaration

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating array of buttons in Visual C++

    I already posted you the "code with declaration"!
    If you are not be able to use it properly - it is your problem! However, you are always welcome to get some help. The only thing you have to do in this case - provide some useful information about your problem and the code being used.

    If you can't let your project work with an array of buttons - please begin with only one button. After such a project will have worked - change the button declaration to the array and try with it.

  12. #12
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Creating array of buttons in Visual C++

    Are we talking about Managed C++/CLI? Then we're in the wrong forum, but anyway:
    Code:
    // create 2d array of buttons
    cli::array<Button^, 2>^ buttons = gcnew cli::array<Button^, 2>(2, 2);
    
    // initialize buttons
    for (int x = 0; x < 2; ++x)
    {
        for (int y = 0; y < 2; ++y)
        {
            buttons[x, y] = gcnew Button();
    
            //...
        }
    }
    - petter

  13. #13
    Join Date
    Dec 2006
    Posts
    11

    Re: Creating array of buttons in Visual C++

    unbelievably...
    but fact
    It's works )))))
    Thank you

  14. #14
    Join Date
    Dec 2006
    Posts
    11

    Re: Creating array of buttons in Visual C++

    Quote Originally Posted by wildfrog
    Are we talking about Managed C++/CLI? Then we're in the wrong forum, but anyway:
    - petter
    Where is it C++/CLI forum?
    I use Visual.Net
    My project in C++ language

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Creating array of buttons in Visual C++

    Quote Originally Posted by alexon1ine
    Where is it C++/CLI forum?
    http://www.codeguru.com/forum/forumd...aysprune=&f=17 - just goto the combobox "Forum Jump" and select "Managed C++ and C++/CLI"
    I use Visual.Net
    My project in C++ language
    Not really. Probably, Managed C++/CLI?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured