Resize Table cell dynamically

Hi,

I am trying to do the following:
I have a 2 by 2 table in VC++2005 and I would like to be able to have a button in each cell that, when clicked, expands the cell to span the entire table and, when clicked again, returns to original (50% by 50%) view.

Basically, I would like to change the ColumnStyle and RowStyle as I see fit at runtime... and have no idea how to do so


Level: NEWBIE in Visual C++ but with a solid background in C/C++

Thanks,
Alex
bluedog666Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
If your table is built using TableLayoutPanel, as I suggested in your previous question, use this code. To test it create new C++/CLI Windows application and paste this code to Form1.h replacing existing code:

#pragma once


namespace test {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TableLayoutPanel^  tableLayoutPanel1;
    protected:
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button3;
    private: System::Windows::Forms::Button^  button4;

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->button4 = (gcnew System::Windows::Forms::Button());
            this->tableLayoutPanel1->SuspendLayout();
            this->SuspendLayout();
            //
            // tableLayoutPanel1
            //
            this->tableLayoutPanel1->ColumnCount = 2;
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
                50)));
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
                50)));
            this->tableLayoutPanel1->Controls->Add(this->button1, 0, 0);
            this->tableLayoutPanel1->Controls->Add(this->button2, 1, 0);
            this->tableLayoutPanel1->Controls->Add(this->button3, 0, 1);
            this->tableLayoutPanel1->Controls->Add(this->button4, 1, 1);
            this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
            this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
            this->tableLayoutPanel1->RowCount = 2;
            this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
            this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
            this->tableLayoutPanel1->Size = System::Drawing::Size(292, 266);
            this->tableLayoutPanel1->TabIndex = 0;
            //
            // button1
            //
            this->button1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->button1->Location = System::Drawing::Point(3, 3);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(140, 127);
            this->button1->TabIndex = 0;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            //
            // button2
            //
            this->button2->Dock = System::Windows::Forms::DockStyle::Fill;
            this->button2->Location = System::Drawing::Point(149, 3);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(140, 127);
            this->button2->TabIndex = 1;
            this->button2->Text = L"button2";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
            //
            // button3
            //
            this->button3->Dock = System::Windows::Forms::DockStyle::Fill;
            this->button3->Location = System::Drawing::Point(3, 136);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(140, 127);
            this->button3->TabIndex = 2;
            this->button3->Text = L"button3";
            this->button3->UseVisualStyleBackColor = true;
            this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
            //
            // button4
            //
            this->button4->Dock = System::Windows::Forms::DockStyle::Fill;
            this->button4->Location = System::Drawing::Point(149, 136);
            this->button4->Name = L"button4";
            this->button4->Size = System::Drawing::Size(140, 127);
            this->button4->TabIndex = 3;
            this->button4->Text = L"button4";
            this->button4->UseVisualStyleBackColor = true;
            this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292, 266);
            this->Controls->Add(this->tableLayoutPanel1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->tableLayoutPanel1->ResumeLayout(false);
            this->ResumeLayout(false);

        }

        bool maximized;

#pragma endregion
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 if ( maximized )
                 {
                     Restore();
                 }
                 else
                 {
                     Maximize(0, 0);
                 }
             }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
             if ( maximized )
             {
                 Restore();
             }
             else
             {
                 Maximize(1, 0);
             }
         }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
             if ( maximized )
             {
                 Restore();
             }
             else
             {
                 Maximize(0, 1);
             }
         }
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
             if ( maximized )
             {
                 Restore();
             }
             else
             {
                 Maximize(1, 1);
             }
         }

         void Maximize(int column, int row)
         {
             int i;

             for ( i = 0; i < tableLayoutPanel1->ColumnCount; i++ )
             {
                 tableLayoutPanel1->ColumnStyles[i]->SizeType = SizeType::Percent;

                 if ( i == column)
                    tableLayoutPanel1->ColumnStyles[i]->Width = 100;
                 else
                     tableLayoutPanel1->ColumnStyles[i]->Width = 0;
             }

             for ( i = 0; i < tableLayoutPanel1->RowCount; i++ )
             {
                 tableLayoutPanel1->RowStyles[i]->SizeType = SizeType::Percent;

                 if ( i == row)
                     tableLayoutPanel1->RowStyles[i]->Height = 100;
                 else
                     tableLayoutPanel1->RowStyles[i]->Height = 0;
             }

             maximized = true;
         }

         void Restore()
         {
             int i;

             for ( i = 0; i < tableLayoutPanel1->ColumnCount; i++ )
             {
                 tableLayoutPanel1->ColumnStyles[i]->SizeType = SizeType::Percent;

                 tableLayoutPanel1->ColumnStyles[i]->Width = 100.0f/tableLayoutPanel1->ColumnCount;
             }

             for ( i = 0; i < tableLayoutPanel1->RowCount; i++ )
             {
                 tableLayoutPanel1->RowStyles[i]->SizeType = SizeType::Percent;

                 tableLayoutPanel1->RowStyles[i]->Height = 100.0f/tableLayoutPanel1->RowCount;
             }

             maximized = false;
         }
    };
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexFMCommented:
Small correction: move maximized variable outside of Windows Form Designer region. This makes code clear: maximized is my variable and not variable generated by code designer.

#pragma endregion

    private:  bool maximized;

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.