Use to make tables and grids which may have any control in each of their cells.

Making Table Properly Resize On Change Of Contents

There seems to be an issue with reducing the number of columsn and rows but the TLP scroll bars remaining at the previous
larger size. A way to get round this is to simply re-create the whole panel.

Making a dynamic run time text box based table layout panel

The following example will work fine as long as you don’t have too many cells. Once you start having a large number (10’s x 10’s) the
re-creation of the grid can be slow. If you want lots x lots forget it as its painfully slow and if you get to more than 10000 individual cells you get a “Error creating window handle” as windows can only create so many individual window controls.

At class level define the array of text boxes that will fill the table grid

	private: array<System::Windows::Forms::TextBox^, 2>  ^txtGridBoxes;
This function creates a grid full of the text boxes

private: void ReDrawGrid (int NumOfColumns, int NumOfRows)
{
	int Count;
	int Column;
	int Row;

	//Stop redrawing while we update contents
	this->SuspendLayout();
	Table1->Visible = false;
	Table1->SuspendLayout();		//Stop painting of the ListBox as items are added

	//--------------------------------------------
	//----- REMOVE THE EXISTING CONTROLS ---------
	//--------------------------------------------
	Table1->Controls->Clear();

	//-----------------------------
	//----- RE-SIZE THE TABLE -----
	//-----------------------------
	Table1->ColumnStyles->Clear();
	Table1->RowStyles->Clear();

	Table1->ColumnCount = NumOfColumns;
	Table1->RowCount = NumOfRows;

	for (Count = 0; Count < NumOfColumns; Count++)
		Table1->ColumnStyles->Add(gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Absolute, 100));	//Set the width

	for (Count = 0; Count < NumOfRows; Count++)
		Table1->RowStyles->Add(gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Absolute, 20));			//Set the height

	//----------------------------
	//----- ADD THE CONTROLS -----
	//----------------------------
	txtGridBoxes = gcnew array<System::Windows::Forms::TextBox^, 2>(NumOfColumns, NumOfRows);

	for (Column = 0; Column < NumOfColumns; Column++)
	{
		for (Row = 0; Row < NumOfRows; Row++)
		{
			txtGridBoxes[Column, Row] = (gcnew System::Windows::Forms::TextBox());
			txtGridBoxes[Column, Row]->Margin = System::Windows::Forms::Padding(0, 0, 0, 0);
			txtGridBoxes[Column, Row]->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			txtGridBoxes[Column, Row]->Dock = System::Windows::Forms::DockStyle::Fill;
			txtGridBoxes[Column, Row]->ReadOnly = true;
			Table1->Controls->Add(txtGridBoxes[Column, Row], Column, Row);
		}
	}

	Table1->Visible = true;
	Table1->ResumeLayout();
	this->ResumeLayout();
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *