Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Creating a document with 'protected areas'

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:Visual Basic, Visual Basic .NET
Version:1.1
Released:December 09, 2002
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Basic
Download code:protected_areas.zip Download [30.18 KB, ZIP]

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.

Please take a look at the following screenshot. To illustrate the protected areas, we have colored their background in gray. This is just for illustrative purposes only - the actual application does not gray-out the protected area.

Visual Basic 6

  1. Private Sub TXTextControl1_KeyPress(KeyAscii As Integer)
  2. If mnu_options_readonly.Checked Then
  3. If Not TXTextControl1.TableAtInputPos <> 0 Then
  4. KeyAscii = 0
  5. Else
  6. If TXTextControl1.TableRowAtInputPos = 1 Then
  7. KeyAscii = 0
  8. End If
  9. End If
  10. End If
  11. End Sub

Visual Basic .NET

  1. Private Sub TxTextControl1_KeyPress(ByVal sender As
  2. Object,ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxTextControl1.KeyPress
  3. If mnuOptions_ReadOnly.Checked Then
  4. Dim table As TXTextControl.Table = TxTextControl1.Tables.GetItem
  5. If IsNothing(table) Then
  6. e.Handled = True
  7. Else
  8. If table.Columns.GetItem.Position =
  9. table.Columns.GetItem(1).Position Then
  10. e.Handled = True
  11. End If
  12. End If
  13. End If
  14. 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:

  1. Private Sub TXTextControl1_KeyDown(KeyCode As Integer, Shift As Integer)
  2. If mnu_options_readonly.Checked Then
  3. If Not TXTextControl1.TableRowAtInputPos > 1 Then
  4. If KeyCode <> vbKeyLeft And KeyCode <> vbKeyRight And KeyCode <>
  5. vbKeyUp And KeyCode <> vbKeyDown Then
  6. KeyCode = 0
  7. End If
  8. End If
  9. End If
  10. End Sub

This sample can be also realized with marked text fields by using the FieldAtInputPos property instead of the TableAtInputPos property.

Complete code in Visual Basic 6 and Visual Basic .NET can be downloaded here:

We would like to offer you as much assistance with TX Text Control as you require. Please post all questions directly into the TX Text Control Support Forum.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place