In this newsletter, we are going to show you how to create statistics about word processing documents in TX Text Control .NET for Windows Forms.
Statistics show interesting information such as how often characters have been typed or how many paragraphs a word processing document contains.
In a previous newsletter, we have presented this feature for TX Text Control ActiveX, however, using TX Text Control .NET for Windows Forms a much easier and at the same time more elegant solution can be created.
Retrieving the number of lines in a document is simple, using the new Line collection. The following code returns the number of lines:
lblLines.Text = tx.Lines.Count.ToString();
For other algorithms, we need to copy the text into a string array, using the Save method to work on this array:
tx.Save(out m_strData, TXTextControl.StringStreamType.PlainText);
To implement a word count algorithm for example, we go through every letter in the buffer and check whether it is a CR, LF or space. If these characters are not found, we know that the current letter belongs to a word. If the last letter is a control character, we know that we have found a new word, thus we increment the counter.
private int CountWords()
{
int Count = 0;
char LastChar = ' ';
for(int i = 0; i < m_strData.Length; i++)
{
if(!IsSpaceRet(m_strData[i]) && !IsControlChar(m_strData[i]))
{
if(IsSpaceRet(LastChar))
Count++;
}
LastChar = m_strData[i];
}
return Count;
}
At least TX Text Control .NET for Windows Forms and Visual Studio .NET is required.
As ever, our support team is waiting to answer your questions.
Best regards
The Newsletter Team