Browse DevX
Sign up for e-mail newsletters from DevX


Enable Your Windows Forms Applications to Drag-and-Drop Data Objects : Page 5

This article demonstrates how to import files from the Windows shell and how to enhance some UI controls to make them accept input via drag-and-drop. Notable examples are the TextBox and the PictureBox controls.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Filling a TextBox
More often than not, drag-and-drop involves moving or copying text from various types of text boxes hosted by different applications, like the Office suite of applications. The TextBox component doesn't natively support drag-and-drop, but can be extended in order to allow drops and to drag text out. Let's see how to tweak a .NET Framework TextBox control so that it can import the text dragged from external applications.

It's a three-step task: set AllowDrop to True on the TextBox; write a DragOver handler to control the drag-over process; and write a DragDrop handler to finalize the whole operation (see Listing 2).

Figure 1. Word Drag/Drop Feedback: Word gives clear feedback about the insertion point of the text.
In the DragOver event, first, check the type of data being dragged. If the drag-and-drop operation is not carrying plain text, the control refuses any drop. Otherwise, it determines the effect of the operation by looking at the state of the keyboard. The default effect, if allowed by the source, is Copy, meaning that the data is copied to the control and not removed at the source. If the CTRL key is held down, and the Move effect is allowed, the effect is set to Move.

If you observe how drag-and-drop works in Microsoft Word, you'll notice that a little caret is displayed close to the mouse pointer to denote the insertion point (see Figure 1).

To obtain a similar effect on a plain TextBox control, you can physically move the caret to the character closest to the mouse pointer. The .NET Framework provides no built-in class or method to read caret information like the current row and column position. You also need to resort to a trick to set the caret position.

TextBox1.SelectionStart = charIndex TextBox1.SelectionLength = 0

Set the SelectionStart property to an integer value that represents the 0-based index of the character in the control's buffer. At the same time, set the length of the selection to 0. As a result, the caret is moved close to the specified character. How can you determine the index of the character at a certain mouse position? Luckily enough, the Win32 Edit window—the Win32 counterpart of the TextBox control—supports the EM_CHARFROMPOS message. This message takes in the coordinates of a point within the TextBox's client area and returns the 0-based index of the nearest character. To send this message to the TextBox, you need to interop with the underlying Win32 infrastructure. The code in Listing 3 shows the interop .NET class that calls into a Win32 DLL.

Figure 2. TextBox Drag/Drop Support: The TextBox control supports text dropping and gives feedback about the insertion points.
The .NET Framework provides full support for calls directed at the Win32 SDK. No matter how optimized the interop layer is, minimizing the number of calls and the quantity of data being marshaled back and forth is an elementary rule of good programming. For this reason, I've written a small Win32 C++ DLL that exposes just one function: GetCaretInfo. The function takes the handle of the TextBox and returns a structure named CARETINFO (see Listing 3).

Internally, the function retrieves the screen coordinates of the point underneath the mouse and translates it into client coordinates in which the origin corresponds to the top-left corner of the TextBox. The coordinates are passed as an argument to the EM_CHARFROMPOS message and the index of the nearest character is returned. This information is packed into the CARETINFO structure and returned. Figure 2 shows how the caret follows the mouse pointer during a drag-and-drop operation.

Figure 2 shows data being dropped from Microsoft Word. As the yellow ListBox demonstrates, the same text is available in various formats, including rich text format and HTML. Which format you choose is completely application-specific. For example, if you're dropping onto a RichTextBox control, select the RichTextFormat option.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date