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

How to convert text to a table

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:Visual Basic .NET
Version:1.0
Released:August 11, 2004
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Studio 2003
Download code:text2table.zip Download [13.04 KB, ZIP]

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 like a comma or a hyphen.

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

Consider the user types some words which are seperated with commas in the text. Then the user selects 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 like a comma.

Visual Basic .NET does not includes a tokenizer like in VC++, so 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 which will be used to fill the table replacing the selected plain text. This will be 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 user to control the table size and the delimiter. The table size will be calculated automatically based on the counted tokens in the selected text, but the user might want to insert a larger table.

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