共用方式為


操作說明:啟用 Windows Forms RichTextBox 控制項的拖放作業

使用 Windows Forms RichTextBox 控制項的拖放作業,可藉由處理 DragEnterDragDrop 事件來完成。 因此,使用 RichTextBox 控制項進行拖放作業相當簡單。

在 RichTextBox 控制項中啟用拖曳作業

  1. AllowDrop 控制項的 RichTextBox 屬性設為 true

  2. DragEnter 事件的事件處理常式中撰寫程式碼。 使用 if 陳述式,以確認拖曳中的資料是屬於可接受的類型 (在此例中為文字)。 DragEventArgs.Effect 屬性可以設定為 DragDropEffects 列舉的任何值。

    Private Sub RichTextBox1_DragEnter(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.DragEventArgs) _
       Handles RichTextBox1.DragEnter  
       If (e.Data.GetDataPresent(DataFormats.Text)) Then  
          e.Effect = DragDropEffects.Copy  
       Else  
          e.Effect = DragDropEffects.None  
       End If  
    End Sub  
    
    private void richTextBox1_DragEnter(object sender,
    System.Windows.Forms.DragEventArgs e)  
    {  
       if (e.Data.GetDataPresent(DataFormats.Text))
          e.Effect = DragDropEffects.Copy;  
       else  
          e.Effect = DragDropEffects.None;  
    }  
    
    private:  
       void richTextBox1_DragEnter(System::Object ^  sender,  
          System::Windows::Forms::DragEventArgs ^  e)  
       {  
          if (e->Data->GetDataPresent(DataFormats::Text))  
             e->Effect = DragDropEffects::Copy;  
          else  
             e->Effect = DragDropEffects::None;  
       }  
    

    (Visual C# 和 Visual C++) 請將下列程式碼置於表單的建構函式中,以登錄事件處理常式。

    this.richTextBox1.DragEnter += new  
        System.Windows.Forms.DragEventHandler  
        (this.richTextBox1_DragEnter);  
    
    this->richTextBox1->DragEnter += gcnew  
       System::Windows::Forms::DragEventHandler  
       (this, &Form1::richTextBox1_DragEnter);  
    
  3. 撰寫程式碼來處理 DragDrop 事件。 使用 DataObject.GetData 方法來擷取正在拖曳的資料。

    在以下範例中,程式碼會將 Text 控制項的 RichTextBox 屬性設為等於正在拖曳的資料。 如果 RichTextBox 控制項中已有文字,拖曳的文字會插入在插入點。

    Private Sub RichTextBox1_DragDrop(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.DragEventArgs) _
       Handles RichTextBox1.DragDrop  
       Dim i As Int16
       Dim s As String  
    
       ' Get start position to drop the text.  
       i = RichTextBox1.SelectionStart  
       s = RichTextBox1.Text.Substring(i)  
       RichTextBox1.Text = RichTextBox1.Text.Substring(0, i)  
    
       ' Drop the text on to the RichTextBox.  
       RichTextBox1.Text = RichTextBox1.Text + _  
          e.Data.GetData(DataFormats.Text).ToString()  
       RichTextBox1.Text = RichTextBox1.Text + s  
    End Sub  
    
    private void richTextBox1_DragDrop(object sender,
    System.Windows.Forms.DragEventArgs e)  
    {  
       int i;  
       String s;  
    
       // Get start position to drop the text.  
       i = richTextBox1.SelectionStart;  
       s = richTextBox1.Text.Substring(i);  
       richTextBox1.Text = richTextBox1.Text.Substring(0,i);  
    
       // Drop the text on to the RichTextBox.  
       richTextBox1.Text = richTextBox1.Text +
          e.Data.GetData(DataFormats.Text).ToString();  
       richTextBox1.Text = richTextBox1.Text + s;  
    }  
    
    private:  
       System::Void richTextBox1_DragDrop(System::Object ^  sender,  
          System::Windows::Forms::DragEventArgs ^  e)  
       {  
          int i;  
          String ^s;  
    
       // Get start position to drop the text.  
       i = richTextBox1->SelectionStart;  
       s = richTextBox1->Text->Substring(i);  
       richTextBox1->Text = richTextBox1->Text->Substring(0,i);  
    
       // Drop the text on to the RichTextBox.  
       String ^str = String::Concat(richTextBox1->Text, e->Data  
       ->GetData(DataFormats->Text)->ToString());
       richTextBox1->Text = String::Concat(str, s);  
       }  
    

    (Visual C# 和 Visual C++) 請將下列程式碼置於表單的建構函式中,以登錄事件處理常式。

    this.richTextBox1.DragDrop += new  
        System.Windows.Forms.DragEventHandler  
        (this.richTextBox1_DragDrop);  
    
    this->richTextBox1->DragDrop += gcnew
       System::Windows::Forms::DragEventHandler  
       (this, &Form1::richTextBox1_DragDrop);  
    

測試應用程式中的拖放功能

  1. 儲存並建置您的應用程式。 在執行時,執行 WordPad。

    WordPad 是由允許拖放作業之 Windows 所安裝的文字編輯器。 存取方式是按一下 [開始] 按鈕、選取 [執行] ,然後在 [執行] WordPad對話方塊中的文字方塊中輸入 並按一下 [確定]

  2. 一旦開啟 WordPad,請於其中輸入文字的字串。 使用滑鼠、選取文字,然後再將選取的文字拖曳到 Windows 應用程式中的 RichTextBox 控制項。

    請注意,當滑鼠指向 RichTextBox 控制項 (然後因此引發 DragEnter 事件) 時,滑鼠指標會變更,且您可以將選取的文字放到 RichTextBox 控制項。

    當您放開滑鼠按鈕時,會放下選取的文字 (也就是,會引發 DragDrop 事件),並插入 RichTextBox 控制項內。

另請參閱