Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-open-a-file-that-is-dropped-on-a-richtextbox-control.md
T
Andy (Steve) De George be103da07e Replace various WPF include files with content (#1335)
* net-current-v30plus-md.md

* net-current-v40plus-md.md

* tla2sharptla-ui-md.md

* tla2sharptla-uiautomation-md.md

* tla2sharptla-winclient-md.md

* tla2sharptla-xaml-md.md

* tlasharptla-ui-md.md

* tlasharptla-uiautomation-md.md

* tlasharptla-winclient-md.md

* tlasharptla-xaml-md.md

* fix warnings
2022-03-16 12:14:11 -04:00

3.1 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Open a File That is Dropped on a RichTextBox Control 03/30/2017
csharp
vb
drag-and-drop [WPF], RichTextBox
RichTextBox [WPF], drag-and-drop
drag-and-drop [WPF], open a dropped file
6bb8bb54-f576-41db-a9a7-24102ddeb490

How to: Open a File That is Dropped on a RichTextBox Control

In Windows Presentation Foundation (WPF), the xref:System.Windows.Controls.TextBox, xref:System.Windows.Controls.RichTextBox, and xref:System.Windows.Documents.FlowDocument controls all have built-in drag-and-drop functionality. The built-in functionality enables drag-and-drop of text within and between the controls. However, it does not enable opening a file by dropping the file on the control. These controls also mark the drag-and-drop events as handled. As a result, by default, you cannot add your own event handlers to provide functionality to open dropped files.

To add additional handling for drag-and-drop events in these controls, use the xref:System.Windows.UIElement.AddHandler%28System.Windows.RoutedEvent%2CSystem.Delegate%2CSystem.Boolean%29 method to add your event handlers for the drag-and-drop events. Set the handledEventsToo parameter to true to have the specified handler be invoked for a routed event that has already been marked as handled by another element along the event route.

Tip

You can replace the built-in drag-and-drop functionality of xref:System.Windows.Controls.TextBox, xref:System.Windows.Controls.RichTextBox, and xref:System.Windows.Documents.FlowDocument by handling the preview versions of the drag-and-drop events and marking the preview events as handled. However, this will disable the built-in drag-and-drop functionality, and is not recommended.

Example

The following example demonstrates how to add handlers for the xref:System.Windows.DragDrop.DragOver and xref:System.Windows.DragDrop.Drop events on a xref:System.Windows.Controls.RichTextBox. This example uses the xref:System.Windows.UIElement.AddHandler%28System.Windows.RoutedEvent%2CSystem.Delegate%2CSystem.Boolean%29 method and sets the handledEventsToo parameter to true so that the events handlers will be invoked even though the xref:System.Windows.Controls.RichTextBox marks these events as handled. The code in the event handlers adds functionality to open a text file that is dropped on the xref:System.Windows.Controls.RichTextBox.

To test this example, drag a text file or a rich text format (RTF) file from Windows Explorer to the xref:System.Windows.Controls.RichTextBox. The file will be opened in the xref:System.Windows.Controls.RichTextBox. If you press the SHIFT key before the dropping the file, the file will be opened as plain text.

[!code-xamlDragDropSnippets#RtbXAML]

[!code-csharpDragDropSnippets#RtbHandlers] [!code-vbDragDropSnippets#RtbHandlers]