Read only text frames in TX Text Control .NET
Blogged by Björn Meyer on March 22, 2007 and tagged with Samples, .NET.
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.





