Read Only Text Frames in TX Text Control .NET for Windows Forms
Text frames position content at fixed document locations such as recipient address blocks on a letter. This code sample creates a ProtectedTextFrame class that inherits from TextFrame and uses the TextFrameActivated event with keystroke interception to block user editing.

Text frames are a very smart way to position text at a specific position in the document. They are commonly used for a recipient address details area on a letter or any other information that must be at a fixed position in a document.
TX Text Control offers properties to avoid the scaling and the movement of the frame:
Sometimes, applications require that such text frames are not editable by the user. In this case, we can use the TextFrameActivated event to set a flag that indicates that a text frame is currently active.
void TX_TextFrameActivated(object sender, TXTextControl.TextFrameEventArgs e)
{
m_trapKeystrokes = true;
}
On the KeyPress event, we can trap the keystrokes, if the mentioned flag has been set to true.
void m_tx_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (m_trapKeystrokes)
e.Handled = true;
}
The TextFrameDeactivated event is used to reset the global flag:
void m_tx_TextFrameDeactivated(object sender, TXTextControl.TextFrameEventArgs e)
{
m_trapKeystrokes = false;
}
The following class is a sample how to inherit from the TextFrame class to create such protected text frames:
class ProtectedTextFrame : TXTextControl.TextFrame
{
private bool m_protected;
private TXTextControl.TextControl m_tx;
private bool m_trapKeystrokes = false;
public ProtectedTextFrame(Size Size, bool Protected, TXTextControl.TextControl TX) : base(Size)
{
m_protected = Protected;
m_tx = TX;
if (m_protected)
{
m_tx.TextFrameActivated += new TXTextControl.TextFrameEventHandler(TX_TextFrameActivated);
m_tx.TextFrameDeactivated += new TXTextControl.TextFrameEventHandler(m_tx_TextFrameDeactivated);
m_tx.KeyPress += new System.Windows.Forms.KeyPressEventHandler(m_tx_KeyPress);
}
}
void m_tx_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (m_trapKeystrokes)
e.Handled = true;
}
void m_tx_TextFrameDeactivated(object sender, TXTextControl.TextFrameEventArgs e)
{
m_trapKeystrokes = false;
}
void TX_TextFrameActivated(object sender, TXTextControl.TextFrameEventArgs e)
{
m_trapKeystrokes = true;
}
public bool Protected
{
get { return m_protected; }
set { m_protected = value; }
}
}
This class can be instantiated like this:
ProtectedTextFrame newFrame = new ProtectedTextFrame(new Size(1000, 1000), true, textControl1);
newFrame.Protected = true;
textControl1.TextFrames.Add(newFrame, -1);
Feel free to contact me, if you want to discuss this approach or, if you have any questions.
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.
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…
Paste Special: The Easy Way to Implement
TX Text Control version 15.0 introduced a ClipboardFormat parameter on the Paste method, enabling native Paste Special functionality. The GetClipboardFormats method returns all available clipboard…
How to Remove All Section Breaks in a Document?
TX Text Control 15.0 adds per-section page column support alongside existing section breaks. To remove all section breaks programmatically, iterate through SectionCollection using…
Batch Printing: How to Print Documents in One Print Job
Batch printing multiple documents as a single print job using TX Text Control relies on a .NET PrintDocument with PrintPage and QueryPageSettings events. Each page is rendered individually via the…
