This week, we are going to show you how to create a document that has 'protected areas'. In this case, a 'protected area' is a part of the document that is read-only. The end-user cannot edit the contents of a protected area, but can still edit the rest of the document.
We are going do realize this effect by trapping the keystrokes to TX Text Control.
Visual Basic .NET
Private Sub TxTextControl1_KeyPress(ByVal sender As
Object,ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxTextControl1.KeyPress
If mnuOptions_ReadOnly.Checked Then
Dim table As TXTextControl.Table = TxTextControl1.Tables.GetItem
If IsNothing(table) Then
e.Handled = True
Else
If table.Columns.GetItem.Position =
table.Columns.GetItem(1).Position Then
e.Handled = True
End If
End If
End If
End Sub
Visual Basic 6
Private Sub TXTextControl1_KeyPress(KeyAscii As Integer)
If mnu_options_readonly.Checked Then
If Not TXTextControl1.TableAtInputPos <> 0 Then
KeyAscii = 0
Else
If TXTextControl1.TableRowAtInputPos = 1 Then
KeyAscii = 0
End If
End If
End If
End Sub
In the above code snippets, the user is only able to insert text into tables and not into the rest of the document (See the grayed-out areas of the above screenshot.)
Additionally, the first row (table header) cannot be edited. This is realized by checking the row at the current input position. The Visual Basic .NET sample shows how to protect a specific column of the table. If the current input position is not inside a table the KeyCode is set to 0. As we trap all keystrokes, the user is not able to insert text outside of a table.
We also have to trap the KeyDown event, because the user must not be able to paste text into the protected parts of the document. On the other hand, the user should be able to move the caret in the text:
Private Sub TXTextControl1_KeyDown(KeyCode As Integer, Shift As Integer)
If mnu_options_readonly.Checked Then
If Not TXTextControl1.TableRowAtInputPos > 1 Then
If KeyCode <> vbKeyLeft And KeyCode <> vbKeyRight And KeyCode <>
vbKeyUp And KeyCode <> vbKeyDown Then
KeyCode = 0
End If
End If
End If
End Sub
This sample can be also realized with marked text fields by using the FieldAtInputPos property instead of the TableAtInputPos property.
Best regards
The Newsletter Team