TX Text Control Text Processing Tutorial (4) Marking Text Fields

original
2014/06/11 12:00
Number of readings 438

Tagged text fields are tags inserted into text, which can be used to achieve a wide range of word processing functions in word processors. For example:

  • Mail Merge
  • Calculated fields in spreadsheets
  • Bookmark
  • Automatically generate table contents and indexes
  • Hypertext links and anchors


Tagged text fields can be composed of any combination of characters. TX can contain up to 65535 tagged text fields. TX will maintain the location and number information of these text fields, and can also load, save, and clipboard operations.
The corresponding source code of this chapter can be found in TX Text Control NET installation directory:

  •         Samples\WinForms\VB.NET\ Printing
  •         Samples\WinForms\CSharp\ Printing


Section 1: Create Markup Text Field
This section mainly demonstrates how to create tag text fields and click events for tag text fields. The sample program includes a menu item [Insert Field!] and two Text Control controls. One is a normal text editing window, and the other is a prompt information window. The following code demonstrates how to create a tag text field:

 


  
  
  1. private   void  mnuInsertField_Click( object  sender, System. EventArgs e)  
  2. {  
  3.     TXTextControl. TextField newField =  new  TXTextControl. TextField();   
  4.     newField. Text =  "--------" ;   
  5.     newField. ID = fieldID;   
  6.     fieldID += 1;   
  7.     textControl1.TextFields. Add(newField);   

Insert a text field at the current input position through the above code, and set a position value for the ID attribute of the text field. When the mouse moves to the text field, Text Control will change the cursor into a hand cursor to prompt the user that there is a text field at the current position. Clicking the text field will trigger the TextFieldClicked event, and a prompt box showing the number of the current text field will pop up.

 


  
  
  1. private   void  textControl1_TextFieldClicked( object  sender, TXTextControl. TextFieldEventArgs e) 
  2. {  
  3.      // Field has been clicked on, update text of second TX and display it   
  4.     textControl2.Text =  "Field clicked, ID: "  + e. TextField.ID;   
  5.     textControl2.BringToFront();   
  6. }  
  7. private   void  textControl1_MouseUp( object  sender, System. Windows.Forms.MouseEventArgs e)  
  8. {  
  9.     textControl1.BringToFront();   

 

Section 2: Bookmarks
This example demonstrates how to use the tag text field to create a bookmark. First, we will use the number of the text field to access the text field. The sample program contains a [Bookmark] menu, including two submenu items [Insert] and [Go to...]. When clicking the [Insert] menu, a text field will be created at the current position; If the selected text is not empty, the selected text will be converted to a marked text field. The code is as follows:

 


  
  
  1. private   void  mnuBookmark_Insert_Click( object  sender, System. EventArgs e)  
  2. {  
  3.      if  (textControl1.Text ==  "" )  
  4.     {  
  5.         MessageBox. Show( "Cannot insert a bookmark if the Text Control is empty." );   
  6.     }  
  7.      else   if  (textControl1.Selection.Length == 0)  
  8.     {  
  9.         textControl1.Selection. Length = 1;   
  10.     }  
  11.      else   
  12.     {  
  13.         TXTextControl. TextField newField =  new  TXTextControl. TextField();   
  14.         newField. ID = fieldID;   
  15.         newField. Text = textControl1.Selection. Text;   
  16.         textControl1.Selection. Text =  "" ;   
  17.         fieldID += 1;   
  18.         textControl1.TextFields. Add(newField);   
  19.     }  

Enter some text in the document and insert some bookmarks, then select the [Go To...] menu, a dialog box will pop up, where you can enter the number of the bookmark. If the bookmark exists, click the [OK] button, it will automatically jump to the location of the bookmark. The actual code is as follows:

 


  
  
  1. private   void  cmdOK_Click( object  sender, System. EventArgs e)  
  2. {  
  3.      if  (Convert.ToInt32(textBox1.Text) >  tx. TextFields.Count)  
  4.     {  
  5.         MessageBox. Show ( "Invalid bookmark number!" );   
  6.     }  
  7.      else   
  8.     {  
  9.          foreach  (TXTextControl.TextField field  in  tx. TextFields)  
  10.         {  
  11.              if  (field.ID == Convert.ToInt32(textBox1.Text))  
  12.             {  
  13.                 tx. Selection.Start = field. Start - 1;   
  14.                 tx. Selection.Length = field. Length;   
  15.             }  
  16.         }  
  17.     }  
  18.     Close();   

Section 3: Assigning Names to Bookmarks
In commercial text editors, bookmarks can be accessed not only by number, but also by bookmark name. The user can specify a name for the bookmark when creating it. In the [GoTo Bookmark] dialog box, the user can select the name of the bookmark, and then jump to the location of the bookmark.
When creating a bookmark, the user needs to specify a name for the bookmark. The implementation code is as follows:

 


  
  
  1. private   void  cmdOK_Click( object  sender, System. EventArgs e)  
  2. {  
  3.     TXTextControl. TextField field =  new  TXTextControl. TextField();  
  4.  
  5.     field. Name = textBox1.Text;   
  6.     field. Text = tx. Selection.Text;   
  7.     tx. Selection.Text =  "" ;  
  8.  
  9.     tx. TextFields.Add(Field);   
  10.     Close();   

Through the above operations, we created a bookmark and specified its name. The names of all bookmarks will be displayed in the Goto Bookmark dialog box. The implementation code is as follows:

 


  
  
  1. private   void  frmGotoDialog_Load( object  sender, System. EventArgs e)  
  2. {  
  3.      foreach  (TXTextControl.TextField Field  in  tx. TextFields) {  
  4.         ComboBox1.Items. Add(Field. Name);   
  5.     }  

When the user clicks [OK] after selecting the bookmark, the bookmark specified by the user will be found by traversing all the bookmarks. The implementation code is as follows:

 


  
  
  1. private   void  cmdOK_Click( object  sender, System. EventArgs e)  
  2. {  
  3.      foreach  (TXTextControl.TextField field  in  tx. TextFields)  
  4.     {  
  5.          if  (field.Name == comboBox1.Text)  
  6.         {  
  7.             tx. Selection.Start = field. Start - 1;   
  8.             tx. Selection.Length = field. Length;   
  9.         }  
  10.     }  
  11.     Close();   

 

TX Text Control Trial Download

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top