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

Drag and drop images into TX Text Control

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:C# .NET
Version:1.0
Released:August 08, 2007
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Studio 2005
Download code:tx_drag_images.zip Download [13.48 KB, ZIP]

TX Text Control implements drag and drop events that can be used to handle almost all commonly used drag and drop approaches, such as opening files when the user drops a file from the File Explorer into TX Text Control.

This basic example illustrates how images can be dropped into TX Text Control, using these events. The images can be dropped into TX Text Control from the Windows File Explorer and will be inserted at the current mouse position.

First, the DragEnter event is used to check whether there are files to be dropped into TX Text Control.

  1. private void textControl1_DragEnter(object sender, DragEventArgs e)
  2. {
  3. if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
  4. e.Effect = DragDropEffects.All;
  5. }

In the DragDrop event, the file names of the files that can be dropped are collected in a string array that is used in a loop to open all images, using the ImageCollection.Add method of TX Text Control. The coordinates that are passed to the event are used to position the image in the document.

  1. private void textControl1_DragDrop(object sender, DragEventArgs e)
  2. {
  3. try
  4. {
  5. string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
  6.  
  7. foreach (string fileName in fileNames)
  8. {
  9. TXTextControl.Image dropImage = new TXTextControl.Image();
  10. dropImage.FileName = fileName;
  11. textControl1.Images.Add(dropImage, new Point((e.X * dpi) _
  12. + textControl1.ScrollLocation.X - (this.Left * dpi), _
  13. (e.Y * dpi) + textControl1.ScrollLocation.Y - (this.Top * dpi)), _
  14. TXTextControl.ImageInsertionMode.DisplaceText);
  15. }
  16. }
  17. catch (Exception exc)
  18. {
  19. throw exc;
  20. }
  21. }

To use the downloadable version of this sample you need TX Text Control .NET 13.0 trial version or better.

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