MeasureString for Text in TextControl
In various applications, it might be neccessary to estimate whether a string with a specific font and size would fit into a fixed space like a table cell. In GDI+, you can use MeasureString to measure a specified string when drawn with a specified Font. In TX Text Control, this is different. TX Text Control has it's own text rendering based on many different factors including a printer driver. This rendering is known as WYSIWYG and makes a rich edit control a good and usable control. But how…


In various applications, it might be neccessary to estimate whether a string with a specific font and size would fit into a fixed space like a table cell. In GDI+, you can use MeasureString to measure a specified string when drawn with a specified Font.
In TX Text Control, this is different. TX Text Control has it's own text rendering based on many different factors including a printer driver. This rendering is known as WYSIWYG and makes a rich edit control a good and usable control.
But how to get the extension of a string without adding the text to the TextControl instance?
Since version X8, the non-visible ServerTextControl can be used in Windows Forms and WPF applications as well. This component can be used to get the length of a text. The following simple function shows how to do that:
///<summary>
///Returns the length of a specific string in Twips.
///</summary>
public int MeasureTextControlString(Selection Selection, string FormattingPrinter)
{
int iLength;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
tx.PageSize.Width = 10000;
tx.FormattingPrinter = FormattingPrinter;
foreach (PropertyInfo property in Selection.GetType().GetProperties())
{
if (property.GetValue(Selection, null).ToString() == "")
continue;
property.SetValue(tx.Selection,
property.GetValue(Selection, null), null);
}
tx.SelectAll();
return iLength = tx.TextChars[tx.Selection.Length].Bounds.Right -
tx.TextChars[tx.Selection.Start + 1].Bounds.Left;
}
}
It is pretty straightforward. A Selection object is copied and applied to the new ServerTextControl instance. The width of the text is calculated using the TextChar bounds of the first and last character in the Selection. The following code shows how to call this function with a new Selection object:
Selection newSelection = new Selection();
newSelection.Text = "Hello dWorld";
newSelection.FontSize = 600;
newSelection.FontName = "Wingdings";
int length = MeasureTextControlString(newSelection, textControl1.FormattingPrinter);
You can specify different properties in the Selection object that are used to calculate the exact width.
Related Posts
Create a Table of Contents in Windows Forms using C#
This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.
Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub
This article gives a quick overview of the new repositories, their structure and our plans for the future.
ASP.NETJavaScriptDocument Editor
Detect Toggle Button Changes Using a MutationObserver
This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…
Two Ways to Restart Numbered Lists in TX Text Control
In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…
Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More
This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.