Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Automatically resize a table when adding columns

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic 6.0
Version:1.0
Released:December 16, 2005
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic 6.0
Download code:tx_resize_table.zip Download [4 KB, ZIP]

Sometimes, you want to insert columns into a table that already exists, but it is not possible to do so, as the table already has the maximum width to fit the page.

The solution is to resize the table automatically, to distribute the available space equally among the columns and to add the new columns afterward. This sample shows you how to do this with TX Text Control ActiveX.

To calculate the new width of each column, the available space (or maximum table width) is calculated and divided by the number of columns present in the table, plus the new columns. Then, the columns are equally resized and the new columns are added. Please note, that the individual size of a column will be lost when using this sample.

  1. Dim newcolumns, i As Integer
  2. 'get columns to be inserted
  3. newcolumns = Val(cboColumns.Text)
  4. Dim width As Integer
  5. 'check if the input position is inside a table
  6. If Form1.TXTextControl1.TableAtInputPos <> 0 Then
  7. 'calculate the new table width including the new columns
  8. width = ((Form1.TXTextControl1.PageWidth - Form1.TXTextControl1.PageMarginL - _
  9. Form1.TXTextControl1.PageMarginR) / (Form1.TXTextControl1.TableColumns _
  10. (Form1.TXTextControl1.TableAtInputPos) + newcolumns))
  11. 'set the new table width
  12. Form1.TXTextControl1.TableCellAttribute(Form1.TXTextControl1.TableAtInputPos, _
  13. 0, 0, txTableCellHorizontalExt) = width
  14. 'check if columns can be inserted
  15. If Form1.TXTextControl1.TableCanInsertColumn = True Then
  16. 'loop for inserting the new columns
  17. For i = 1 To newcolumns
  18. 'insert new column (combox index + 1)
  19. Form1.TXTextControl1.TableInsertColumn (cboPosition.ListIndex + 1)
  20. Next
  21. End If
  22. End If

The minimum requirements for this sample application are TX Text Control ActiveX trial version and Visual Studio 6.0.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place