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

Drag and Drop Text in TX Text Control .NET

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:Visual C#
Version:1.0
Released:November 12, 2007
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual C#
Download code:tx_text_tricks.zip Download [10.15 KB, ZIP]

Dragging and dropping text can be helpful to rearrange large portions of text. It is a very easy way to move text around in word processing applications.

TX Text Control provides all the events needed to implement this behavior. This sample uses 3 different events to control drag and drop: MouseDown, MouseUp and MouseMove.

On the MouseUp event, the selected text is stored in a separate Selection object that holds the currently selected text, it's character index start position and length.

  1. private void textControl1_MouseUp(object sender, MouseEventArgs e)
  2. {
  3. if (dragAction == false)
  4. {
  5. fillSyncSelection();
  6. }
  7. ...
  1. private void fillSyncSelection()
  2. {
  3. syncSelection.Start = textControl1.Selection.Start;
  4. syncSelection.Length = textControl1.Selection.Length;
  5. syncSelection.Text = textControl1.Selection.Text;
  6. }

If the user clicks into the selected text, a boolean flag is activated. This flag indicates that a drag and drop action is currently being performed:

  1. private void textControl1_MouseDown(object sender, MouseEventArgs e)
  2. {
  3. if (syncSelection.Length > 0 && (textControl1.Selection.Start >=
  4. syncSelection.Start && textControl1.Selection.Start <=
  5. syncSelection.Start + syncSelection.Length))
  6. {
  7. dragAction = true;
  8. textControl1.CaretWidth = 2;
  9. }
  10. }

In the MouseMove event, the input position is set to the current mouse position.

  1. private void textControl1_MouseMove(object sender, MouseEventArgs e)
  2. {
  3. if( dragAction == true)
  4. {
  5. textControl1.Selection.Length = 0;
  6. textControl1.InputPosition = new TXTextControl.InputPosition(new Point((e.X * DPIX) +
  7. textControl1.ScrollLocation.X, (e.Y * DPIX) +
  8. textControl1.ScrollLocation.Y));
  9. }
  10. }

When the mouse button is released again, the selected text is copied to the current input position and removed from the original position:

  1. private void textControl1_MouseUp(object sender, MouseEventArgs e)
  2. {
  3. ...
  4. else
  5. {
  6. int start = syncSelection.Start;
  7.  
  8. if (textControl1.Selection.Start < syncSelection.Start)
  9. {
  10. start = syncSelection.Start + syncSelection.Text.Length;
  11. }
  12.  
  13. textControl1.Selection.Text = syncSelection.Text;
  14. int inputPos = textControl1.Selection.Start;
  15. textControl1.Select(start, syncSelection.Length);
  16. textControl1.Selection.Text = "";
  17. textControl1.Selection.Start = inputPos;
  18. textControl1.CaretWidth = 1;
  19. syncSelection = new TXTextControl.Selection();
  20. dragAction = false;
  21. }
  22. }

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

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