Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
RichTextBox Class
 AllowDrop Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
RichTextBox..::.AllowDrop Property

Updated: November 2007

Gets or sets a value indicating whether the control will enable drag-and-drop operations.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public Overrides Property AllowDrop As Boolean
Visual Basic (Usage)
Dim instance As RichTextBox
Dim value As Boolean

value = instance.AllowDrop

instance.AllowDrop = value
C#
[BrowsableAttribute(false)]
public override bool AllowDrop { get; set; }
Visual C++
[BrowsableAttribute(false)]
public:
virtual property bool AllowDrop {
    bool get () override;
    void set (bool value) override;
}
J#
/** @property */
/** @attribute BrowsableAttribute(false) */
public boolean get_AllowDrop()
/** @property */
/** @attribute BrowsableAttribute(false) */
public  void set_AllowDrop(boolean value)
JScript
public override function get AllowDrop () : boolean
public override function set AllowDrop (value : boolean)

Property Value

Type: System..::.Boolean

true if drag-and-drop is enabled in the control; otherwise, false.

The following code example demonstrates how to perform drag-and-drop operations using a ListBox control that contains items to drop into a RichTextBox control. The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. The example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. The DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. This example requires that the DragDrop and DragEnter events have been connected to the event handlers defined in the example.

Visual Basic
Public Sub New()
   MyBase.New()

   'This call is required by the Windows Form Designer.
   InitializeComponent()

   richTextBox1.AllowDrop = True

End Sub

Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listBox1.MouseDown
   ' Determines which item was selected.
   Dim lb As ListBox = CType(sender, ListBox)
   Dim pt As New Point(e.X, e.Y)
   'Retrieve the item at the specified location within the ListBox.
   Dim index As Integer = lb.IndexFromPoint(pt)

   ' Starts a drag-and-drop operation.
   If index >= 0 Then
      ' Retrieve the selected item text to drag into the RichTextBox.
      lb.DoDragDrop(lb.Items(index).ToString(), DragDropEffects.Copy)
   End If
End Sub 'listBox1_MouseDown


Private Sub richTextBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragEnter
   ' If the data is text, copy the data to the RichTextBox control.
   If e.Data.GetDataPresent("Text") Then
      e.Effect = DragDropEffects.Copy
   End If
End Sub 'richTextBox1_DragEnter

Private Sub richTextBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles richTextBox1.DragDrop
   ' Paste the text into the RichTextBox where at selection location.
   richTextBox1.SelectedText = e.Data.GetData("System.String", True).ToString()
End Sub 'richTextBox1_DragDrop


C#
   public Form1()
   {
      InitializeComponent();
      // Sets the control to allow drops, and then adds the necessary event handlers.
      this.richTextBox1.AllowDrop = true;
   }
    
   private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   {
      // Determines which item was selected.
      ListBox lb =( (ListBox)sender);
      Point pt = new Point(e.X,e.Y);
      //Retrieve the item at the specified location within the ListBox.
      int index = lb.IndexFromPoint(pt);

      // Starts a drag-and-drop operation.
      if(index>=0) 
      {
         // Retrieve the selected item text to drag into the RichTextBox.
         lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Copy);
      }
   }

   private void richTextBox1_DragEnter(object sender, DragEventArgs e)
   {
      // If the data is text, copy the data to the RichTextBox control.
      if(e.Data.GetDataPresent("Text"))
         e.Effect = DragDropEffects.Copy;
   }

   private void richTextBox1_DragDrop(object sender, DragEventArgs e) 
   {
      // Paste the text into the RichTextBox where at selection location.
      richTextBox1.SelectedText =  e.Data.GetData("System.String", true).ToString();
   }


Visual C++
public:
   Form1()
   {
      InitializeComponent();

      // Sets the control to allow drops, and then adds the necessary event handlers.
      this->richTextBox1->AllowDrop = true;
   }

private:
   void listBox1_MouseDown( Object^ sender, System::Windows::Forms::MouseEventArgs^ e )
   {
      // Determines which item was selected.
      ListBox^ lb = (dynamic_cast<ListBox^>(sender));
      Point pt = Point(e->X,e->Y);

      //Retrieve the item at the specified location within the ListBox.
      int index = lb->IndexFromPoint( pt );

      // Starts a drag-and-drop operation.
      if ( index >= 0 )
      {
         // Retrieve the selected item text to drag into the RichTextBox.
         lb->DoDragDrop( lb->Items[ index ]->ToString(), DragDropEffects::Copy );
      }
   }

   void richTextBox1_DragEnter( Object^ /*sender*/, DragEventArgs^ e )
   {
      // If the data is text, copy the data to the RichTextBox control.
      if ( e->Data->GetDataPresent( "Text" ) )
            e->Effect = DragDropEffects::Copy;
   }

   void richTextBox1_DragDrop( Object^ /*sender*/, DragEventArgs^ e )
   {
      // Paste the text into the RichTextBox where at selection location.
      richTextBox1->SelectedText = e->Data->GetData( "System.String", true )->ToString();
   }

J#
public Form1()
{
    InitializeComponent();
    //Sets the control to allow drops, and then adds the necessary 
    //event handlers.
    this.richTextBox1.set_AllowDrop(true);
} //Form1

private void listBox1_MouseDown(Object sender, System.Windows.Forms.
    MouseEventArgs e)
{
    //Determines which item was selected.
    ListBox lb =(ListBox)sender;
    Point pt = new Point(e.get_X(), e.get_Y());
    //Retrieve the item at the specified location within the ListBox.
    int index = lb.IndexFromPoint(pt);
    //Starts a drag-and-drop operation.
    if (index >= 0) {
        //Retrieve the selected item text to drag into the RichTextBox.
        lb.DoDragDrop(lb.get_Items().get_Item(index).ToString(), 
            DragDropEffects.Copy);
    }
} //listBox1_MouseDown

private void richTextBox1_DragEnter(Object sender, DragEventArgs e)
{
    // If the data is text, copy the data to the RichTextBox control.
    if (e.get_Data().GetDataPresent("Text")) {
        e.set_Effect(DragDropEffects.Copy);
    }
} //richTextBox1_DragEnter

private void richTextBox1_DragDrop(Object sender, DragEventArgs e)
{
    // Paste the text into the RichTextBox where at selection location.
    richTextBox1.set_SelectedText(e.get_Data().GetData("System.String", true).
        ToString());
} //richTextBox1_DragDrop

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker