RichTextBox.AllowDrop Propiedad

Definición

Obtiene o establece un valor que indica si el control habilitará las operaciones de arrastrar y colocar.Gets or sets a value indicating whether the control will enable drag-and-drop operations.

public:
 virtual property bool AllowDrop { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(false)]
public override bool AllowDrop { get; set; }
public override bool AllowDrop { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.AllowDrop : bool with get, set
member this.AllowDrop : bool with get, set
Public Overrides Property AllowDrop As Boolean

Valor de propiedad

Boolean

Es true si están habilitadas las operaciones de arrastrar y colocar en el control; de lo contrario, es false.true if drag-and-drop is enabled in the control; otherwise, false.

Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo realizar operaciones de arrastrar y colocar con un ListBox control que contiene elementos que se van a colocar en un RichTextBox control.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. El constructor del formulario establece la AllowDrop propiedad en true para permitir que se produzcan las operaciones de arrastrar y colocar en RichTextBox .The constructor of the form sets the AllowDrop property to true to enable drag-and-drop operations to occur in the RichTextBox. En el ejemplo se utiliza el MouseDown evento de ListBox para iniciar la operación de arrastre llamando al DoDragDrop método.The example uses the MouseDown event of the ListBox to start the drag operation by calling the DoDragDrop method. En el ejemplo se utiliza el DragEnter evento para determinar si un elemento que se arrastra en RichTextBox es un tipo de datos válido.The example uses the DragEnter event to determine if an item being dragged into the RichTextBox is a valid data type. El DragDrop evento realiza la colocación real de un elemento arrastrado en el RichTextBox control en la ubicación actual del cursor dentro de RichTextBox .The DragDrop event performs the actual dropping of a dragged item into the RichTextBox control at the current cursor location within the RichTextBox. Este ejemplo requiere que los DragDrop DragEnter eventos y se hayan conectado a los controladores de eventos definidos en el ejemplo.This example requires that the DragDrop and DragEnter events have been connected to the event handlers defined in the example.

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();
   }
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();
}

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


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

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

Se aplica a