TX Text Control ActiveX Documentation

Drag and Drop

This example shows how to use the InputPosFromPoint method to realise a simple Drag&Drop in a Text Control application.

Drag&Drop in a text editor enables the user to drag a piece of text and drop it in a new location of the document. So, the incoming mouse events have to be analyzed and handled.

The code shown here is contained in the Samples\vb6\DragDrop sample source directory.

The Sample Program

In the MouseDown event, the InputPosFromPoint method is used to get the character position the user has clicked on. The current input position and the length of the selection are stored in global variables, because they are needed in the MouseUp event. If the input position the user has clicked on, is inside of the current selection, dragging can be started. First a global variable named dragging is set to true and the MousePointer property is changed to indicate that dragging is in process. The text and format information of the current selection is copied to a memory buffer using the SaveToMemory method. Finally, the Text Control's EditMode property is set to 2 - read only.

[Visual Basic 6] 'Get current input position and the current selection pos = TXTextControl1.InputPosFromPoint(X, Y) gblStart = TXTextControl1.SelStart gblLength = TXTextControl1.SelLength 'Check if the click occured in the current selection If gblStart <= pos And gblStart + gblLength > pos Then 'Start dragging data = TXTextControl1.SaveToMemory(3, True) dragging = True MousePointer = 2 TXTextControl1.EditMode = 2 End If

In the MouseUp event procedure the InputPosFromPoint method is used again to get the character position where the user has left the mouse button. When dragging is in process and the input position is not inside the current selection, the drop operation can be performed. The previously saved text now is inserted with the LoadFromMemory method after setting the new input position with the SelStart property.

[Visual Basic 6] pos = TXTextControl1.InputPosFromPoint(X, Y) If dragging Then 'Check if the new input position is outside of 'the current selection. If it's not, do not 'copy the text If Not (gblStart<=pos And gblStart + gblLength>pos) Then TXTextControl1.SelText = "" If pos < gblStart Then TXTextControl1.SelStart = pos Else TXTextControl1.SelStart = pos - gblLength End If TXTextControl1.LoadFromMemory data, 3, True End If 'End dragging dragging = False MousePointer = vbNormal TXTextControl1.EditMode = 0 End if
 
 
 

Products

Support

Downloads

Corporate

Buy Now