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

Using Drag and Drop in TX Text Control .NET

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:Visual Basic .NET
Version:1.0
Released:March 01, 2006
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Basic .NET
Download code:drag_drop_treeview.zip Download [8.9 KB, ZIP]
Click to enlarge

This sample application shows you how to implement drag and drop in TX Text Control.

In order to demonstrate the capabilities of TX Text Control, we are going to use a treeview, which holds two types of nodes:

Text snippets, which are simple texts for documents and Fields, which can be used for mail merge processes.

If you drag and drop a text snippet, TX Text Control inserts the text at the input position; if you drag and drop a field, TX Text Control inserts a textfield.

Calculating the input position from the DragOver event is quite complicated, as the InputPosition class expects the location in twips, but the event supplies pixel.

To calculate the correct position, at which the text should be dropped or at which the field should be inserted, we need the DPI of the current monitor:

  1. Private Sub Form1_Paint1(ByVal sender As Object, _
  2. ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  3. 'This event is needed to get the DPI of the current device
  4. 'e.g. monitor, to calculate the twips
  5. Dim g As System.Drawing.Graphics = e.Graphics
  6. dpiX = g.DpiX
  7. dpiY = g.DpiY
  8. End Sub

After that, the following conversion is needed and we can finally calculate the input position.
To be able to set the InputPosition on every page, the ScrollLocation must be added.

  1. 'This whole calculation is needed to convert the pixels from e object to twips
  2. Dim pt As New System.Drawing.Point
  3.  
  4. 'Add scroll offset to InputPosition
  5. Dim offsetY As Integer = TextControl1.ScrollLocation.Y
  6. Dim offsetX As Integer = TextControl1.ScrollLocation.X
  7.  
  8. pt = TextControl1.PointToClient(New Point(e.X, e.Y))
  9. pt.X = (pt.X * CInt((1440 / dpiX))) + offsetX
  10. pt.Y = (pt.Y * CInt((1440 / dpiY))) + offsetY
  11. 'Set input position in TX Text Control
  12. TextControl1.InputPosition = New TXTextControl.InputPosition(pt)
  13. 'Put the focus to TX Text Control to display the caret
  14. TextControl1.Focus()

Now we have the correct input position and can insert the text from the dragged node.
We must differentiate two cases, mentioned above:

Insert a textfield, if the text starts with "[" or insert the plain text:

  1. Private Sub TextControl1_DragDrop(ByVal sender As Object, _
  2. ByVal e As System.Windows.Forms.DragEventArgs) Handles TextControl1.DragDrop
  3. 'Get the text from the dragged object
  4. Dim myText As String = e.Data.GetData(DataFormats.Text, False)
  5. 'Check if the text starts with "["
  6. If myText.StartsWith("[") Then
  7. 'If yes, a field must be inserted
  8. Dim myTextField As New TXTextControl.TextField(myText)
  9. myTextField.ShowActivated = True
  10. myTextField.DoubledInputPosition = True
  11. TextControl1.TextFields.Add(myTextField)
  12. Else
  13. 'If not, just put the text at the input position
  14. TextControl1.Selection.Text = myText
  15. End If
  16. TextControl1.Focus() 'Put the focus back to TX Text Control
  17. End Sub

The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio 2003.

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