| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| 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 |

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:
Private Sub Form1_Paint1(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint 'This event is needed to get the DPI of the current device 'e.g. monitor, to calculate the twips Dim g As System.Drawing.Graphics = e.Graphics dpiX = g.DpiX dpiY = g.DpiY 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.
'This whole calculation is needed to convert the pixels from e object to twips Dim pt As New System.Drawing.Point 'Add scroll offset to InputPosition Dim offsetY As Integer = TextControl1.ScrollLocation.Y Dim offsetX As Integer = TextControl1.ScrollLocation.X pt = TextControl1.PointToClient(New Point(e.X, e.Y)) pt.X = (pt.X * CInt((1440 / dpiX))) + offsetX pt.Y = (pt.Y * CInt((1440 / dpiY))) + offsetY 'Set input position in TX Text Control TextControl1.InputPosition = New TXTextControl.InputPosition(pt) 'Put the focus to TX Text Control to display the caret 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:
Private Sub TextControl1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles TextControl1.DragDrop 'Get the text from the dragged object Dim myText As String = e.Data.GetData(DataFormats.Text, False) 'Check if the text starts with "[" If myText.StartsWith("[") Then 'If yes, a field must be inserted Dim myTextField As New TXTextControl.TextField(myText) myTextField.ShowActivated = True myTextField.DoubledInputPosition = True TextControl1.TextFields.Add(myTextField) Else 'If not, just put the text at the input position TextControl1.Selection.Text = myText End If TextControl1.Focus() 'Put the focus back to TX Text Control End Sub
The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio 2003.