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.
While (minLine <= maxLine)
st = New StringTokenizer(tx.Lines.Item(minLine).Text, delimiter)
If (st.CountTokens() > maxItems) Then
maxItems = st.CountTokens
minLine = minLine + 1
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.
For Each line In Lines
x = 1
For Each word In line
tx.Tables.GetItem(nextTable).Cells.GetItem(y, x).Text = word
x = x + 1
y = y + 1
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.
The minimum requirements for this sample application are TX Text Control .NET for Windows Forms trial version and Visual Studio .NET 2003.
The Newsletter Team