RichTextBox.AllowDrop 속성

정의

컨트롤에서 끌어서 놓기 작업을 수행할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

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

속성 값

컨트롤에서 끌어서 놓기 작업을 수행할 수 있으면 true이고, 그렇지 않으면 false입니다.

특성

예제

다음 코드 예제에서는 끌어서 놓을 항목을 포함 하는 컨트롤을 ListBox 사용 하 여 끌어서 놓기 작업을 수행 하는 방법을 보여 줍니다는 컨트롤입니다 RichTextBox . 폼의 생성자는 속성을 true 로 설정 AllowDrop 하여 에서 RichTextBox끌어서 놓기 작업을 수행할 수 있도록 합니다. 예제에서는 합니다 MouseDown 의 이벤트를 ListBox 호출 하 여 끌기 작업을 시작 하려면를 DoDragDrop 메서드. 이 예제에서는 이벤트를 사용하여 DragEnter 로 끌어가 RichTextBox 는 항목이 유효한 데이터 형식인지 확인합니다. 이벤트는 DragDrop 내의 현재 커서 위치에서 컨트롤에 RichTextBox 끌어 놓은 항목의 실제 삭제를 RichTextBox수행합니다. 이 예제에서는 DragDropDragEnter 이벤트가 예제에 정의된 이벤트 처리기에 연결되어야 합니다.

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

적용 대상