TX Text Control Source Code Library

TX Text Control Format Painter

Sometimes, it is very helpful to be able to copy and paste text formatting, so you don't have to memorize all the formatting attributes.

Applying a certain text formatting on existing text can come in handy if you have to format text in several places in a document.

This sample project uses TX Text Control to provide a similar feature as Microsoft Word's Format Painter.

To copy a text format, we use the Selection class to query all needed format attributes, like bold, italic, and so on. To store the values, we use an ArrayList, so we can easily query and apply the values on another selection.

ArrayList myFormatAL = new ArrayList(); private void copyFormat() { //check if text format can be copied, e.g. if the formatting is the same for all characters if (textControl1.Selection.IsCommonValueSelected(TXTextControl.Selection.Attribute.All)) { //clear the ArrayList before using it myFormatAL.Clear(); //copy each formatting attribute myFormatAL.Add(textControl1.Selection.Baseline); myFormatAL.Add(textControl1.Selection.Bold); myFormatAL.Add(textControl1.Selection.FontName); myFormatAL.Add(textControl1.Selection.FontSize); myFormatAL.Add(textControl1.Selection.ForeColor); myFormatAL.Add(textControl1.Selection.Italic); myFormatAL.Add(textControl1.Selection.Strikeout); myFormatAL.Add(textControl1.Selection.TextBackColor); myFormatAL.Add(textControl1.Selection.Underline); } }

An important part is to check if all characters have the same format, otherwise it is not possible to get a defined value. This is possible using the IsCommonValueSelected function:

//check if text format can be copied, e.g. if the formatting is the same for all characters if (textControl1.Selection.IsCommonValueSelected(TXTextControl.Selection.Attribute.All)) [...]

We always recommend working with styles instead of formatting a document by hand.We even included a method to create a (custom) style from text formatting. To keep it simple, we are using the word "custom" and a number for the style name, but this can of course be changed.

//create a new custom style based on the stored formatting attributes TXTextControl.InlineStyle iStyle = new TXTextControl.InlineStyle("custom" + customStyle.ToString()); iStyle.Baseline = textControl1.Selection.Baseline; iStyle.Bold = textControl1.Selection.Bold; iStyle.FontName = textControl1.Selection.FontName; iStyle.FontSize = textControl1.Selection.FontSize; iStyle.ForeColor = textControl1.Selection.ForeColor; iStyle.Italic = textControl1.Selection.Italic; iStyle.Strikeout = textControl1.Selection.Strikeout; iStyle.TextBackColor = textControl1.Selection.TextBackColor; iStyle.Underline = textControl1.Selection.Underline; //add the new custom style to the TextControl textControl1.InlineStyles.Add(iStyle); customStyle += 1; //use the new custom style for the current selection textControl1.Selection.FormattingStyle = iStyle.Name;

To ensure that we can copy or paste a text format, we use some checks in the formatPaintToolStripMenuItem_DropDownOpening:

copyToolStripMenuItem2.Enabled = textControl1.Selection.IsCommonValueSelected(TXTextControl.Selection.Attribute.All); createStyleFromFToolStripMenuItem.Enabled = textControl1.Selection.IsCommonValueSelected(TXTextControl.Selection.Attribute.All); if ((textControl1.Selection.Length == 0) | (myFormatAL.Count < 1)) { pasteFormatToolStripMenuItem.Enabled = false; } else { pasteFormatToolStripMenuItem.Enabled = true; }

The minimum requirements for this sample application are TX Text Control .NET for Windows Forms trial version 14 and Visual Studio 2005.

 
 

Products

Support

Downloads

Corporate

Buy Now