Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.

Convert text to a table using TX Text Control .NET

Topics for Tuesday, August 10 2004

Convert text to a table using TX Text Control .NET

Click to enlarge

An often requested feature is the 'Text to Table' functionality from MS Word, which converts plain text into a table using delimiters, such as commas or hyphens.

Today, we would like to show you how to implement this into your TX Text Control based word processing application.

Our application will allow an end user to type some words, seperated by a comma, in to a word processing document.Then s/he is able to select the lines and click on a menu entry to convert them into a table automatically.

We use the Lines Collection of TX Text Control to read the selected text row by row. This string is used in a tokenizer, which splits the string into small pieces based on a delimiter such as a comma.

Visual Basic .NET does not includes a tokenizer like the one in VC++, thus we have to write our own. We have placed the class StringTokenizer into the downloadable archive, but not described it here. It returns all tokens in a string, the lines of the TX Text Control.

  1. While (minLine <= maxLine)
  2. st = New StringTokenizer(tx.Lines.Item(minLine).Text, delimiter)
  3.  
  4. If (st.CountTokens() > maxItems) Then
  5. maxItems = st.CountTokens
  6. End If
  7.  
  8. Lines.Add(st.Tokens)
  9. minLine = minLine + 1
  10. End While

We fill a Collection with all tokens that will be used to fill the table, replacing the selected plain text. This is done by looping through the whole Lines Collection. We use the Table.Cell Collection to insert the text into the appropriate cells.

  1. For Each line In Lines
  2. x = 1
  3. For Each word In line
  4. tx.Tables.GetItem(nextTable).Cells.GetItem(y, x).Text = word
  5. x = x + 1
  6. Next
  7. y = y + 1
  8. Next

Additionally, we have implemented a simple dialog, which allows the end user to control the table size and the delimiter. The table size is calculated automatically based on the counted tokens in the selected text. The end user, however, is still able to insert a larger table.

Please download Download [13.04 KB, ZIP] the full source code for this sample.

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

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