Solved

Resize Table cell dynamically

Posted on 2006-07-01
2
809 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:bluedog666
  • 2
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17025896
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17025905
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now