In this newsletter, we are going to show you some interesting tricks using styles in TX Text Control .NET for Windows Forms
Thanks to the new class oriented design of TX Text Control .NET for Windows Forms, manipulating the internal styles of TX Text Control has become very easy. The shipped button bar includes a combo box to select and apply every style to a paragraph. Many customers, however, would like to be able to offer their end-users a style selection box that is not part of the button bar.
In this newsletter, we are going to show you how to fill in a combo box with the styles available in TX Text Control. We use a dialog form to create a "style selector" where the user can choose a style and apply it to specific paragraphs.
Using the ParagraphStyleCollection and a simple Visual Basic "For Each" statement, it is very simple to fill a list box with the styles.
Dim style As TXTextControl.FormattingStyle
For Each style In TextControl1.ParagraphStyles
Every available style is displayed in the style selector form and the user can choose one of the styles. In this case, the whole paragraph is selected, so that the selected style can be applied to the complete paragraph. This is done by searching for carriage returns in the text to get to the start and to the end positions of the current paragraph.
paragraphStart = tx.Find(Chr(10), curPos, 17)
paragraphEnd = tx.Find(Chr(10), curPos, 16)
Additionally, the user should be able to see the font settings of the selected style in a preview window. For this reason, we use a second TX Text Control and copy the selected paragraph into the new TX Text Control. The font attributes can be simply applied using the selected style object:
TextControl1.Selection.FontName = tx.ParagraphStyles.GetItem(ListBox1.SelectedItem).FontName
Finally, if the user presses the button "Apply", the selected style will be applied to the specified paragraph.
tx.Selection.FormattingStyle = ListBox1.SelectedItem
The Newsletter Team